RE: Problem resizing a window and button placement
So, how do I use the width value in my code? SGA -Original Message- From: Python-list On Behalf Of MRAB via Python-list Sent: Saturday, February 24, 2024 10:36 PM To: python-list@python.org Subject: Re: Problem resizing a window and button placement On 2024-02-25 02:51, Steve GS wrote: > import tkinter as tk > > #global Ww Neither global helps > def on_configure(*args): > # print(args) > #global Ww Neither global helps > Ww = root.winfo_width() > print("WwInside = <" + str(Ww) + ">") > > root = tk.Tk() > root.bind('', on_configure) print("WwOutside = <" + str(Ww) > + ">") > #NameError: name 'Ww' is not defined > root.mainloop() 'Ww' won't exist until 'on_configure' assigns to it, and that won't happen until `mainloop` starts. Also, 'global' works only within a function. 8< import tkinter as tk def on_configure(event): print(f'{event.width=}, {event.height=}') root = tk.Tk() root.bind('',on_configure) root.mainloop() 8< -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
RE: Problem resizing a window and button placement
The print statement in the function prints. Does that not mean that the function is being called? SGA -Original Message- From: Python-list On Behalf Of Thomas Passin via Python-list Sent: Saturday, February 24, 2024 10:39 PM To: python-list@python.org Subject: Re: Problem resizing a window and button placement On 2/24/2024 9:51 PM, Steve GS via Python-list wrote: First of all, please make sure that the formatting is readable and especially the indentation. This is Python, after all. Do not use tabs; use 3 or 4 spaces instead of each tab. > import tkinter as tk > > #global Ww Neither global > helps > def on_configure(*args): > # print(args) > #global Ww Neither > global helps > Ww = root.winfo_width() > print("WwInside = <" + > str(Ww) + ">") > > root = tk.Tk() > root.bind('', > on_configure) > print("WwOutside = <" + > str(Ww) + ">") > #NameError: name 'Ww' is not > defined The function that declares Ww hasn't run yet. As I wrote earlier, the function bound to the callback should do all the work for the callback, or it should call other functions that do. That's if you don't let a layout do it all for you, as others have written. > root.mainloop() > > SGA > > -Original Message- > From: Python-list > sga.ni...@python.org> On > Behalf Of MRAB via Python-list > Sent: Saturday, February 24, > 2024 7:49 PM > To: python-list@python.org > Subject: Re: Problem resizing > a window and button placement > > On 2024-02-25 00:33, Steve GS > via Python-list wrote: >> "Well, yes, in Python a >> variable created inside a >> function or method is local > to >> that function unless you >> declare it global." >> >> Yes, I knew that. I tried to >> global it both before the >> function call and within it. >> Same for when I created the >> variable. If I try to use it >> in the rest of the code, it >> keeps coming up as not >> declared. In other > functions, >> I can 'return' the variable >> but that apparently would > not >> work for this function. >> >> Is this type of function any >> different that that which I >> have been using? >> > Please post a short example > that shows the problem. > -- https://mail.python.org/mailma n/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem resizing a window and button placement
On 2024-02-25 02:51, Steve GS wrote: import tkinter as tk #global Ww Neither global helps def on_configure(*args): # print(args) #global Ww Neither global helps Ww = root.winfo_width() print("WwInside = <" + str(Ww) + ">") root = tk.Tk() root.bind('', on_configure) print("WwOutside = <" + str(Ww) + ">") #NameError: name 'Ww' is not defined root.mainloop() 'Ww' won't exist until 'on_configure' assigns to it, and that won't happen until `mainloop` starts. Also, 'global' works only within a function. 8< import tkinter as tk def on_configure(event): print(f'{event.width=}, {event.height=}') root = tk.Tk() root.bind('',on_configure) root.mainloop() 8< -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem resizing a window and button placement
On 2/24/2024 9:51 PM, Steve GS via Python-list wrote: First of all, please make sure that the formatting is readable and especially the indentation. This is Python, after all. Do not use tabs; use 3 or 4 spaces instead of each tab. import tkinter as tk #global Ww Neither global helps def on_configure(*args): # print(args) #global Ww Neither global helps Ww = root.winfo_width() print("WwInside = <" + str(Ww) + ">") root = tk.Tk() root.bind('', on_configure) print("WwOutside = <" + str(Ww) + ">") #NameError: name 'Ww' is not defined The function that declares Ww hasn't run yet. As I wrote earlier, the function bound to the callback should do all the work for the callback, or it should call other functions that do. That's if you don't let a layout do it all for you, as others have written. root.mainloop() SGA -Original Message- From: Python-list On Behalf Of MRAB via Python-list Sent: Saturday, February 24, 2024 7:49 PM To: python-list@python.org Subject: Re: Problem resizing a window and button placement On 2024-02-25 00:33, Steve GS via Python-list wrote: "Well, yes, in Python a variable created inside a function or method is local to that function unless you declare it global." Yes, I knew that. I tried to global it both before the function call and within it. Same for when I created the variable. If I try to use it in the rest of the code, it keeps coming up as not declared. In other functions, I can 'return' the variable but that apparently would not work for this function. Is this type of function any different that that which I have been using? Please post a short example that shows the problem. -- https://mail.python.org/mailman/listinfo/python-list
RE: Problem resizing a window and button placement
import tkinter as tk #global Ww Neither global helps def on_configure(*args): # print(args) #global Ww Neither global helps Ww = root.winfo_width() print("WwInside = <" + str(Ww) + ">") root = tk.Tk() root.bind('', on_configure) print("WwOutside = <" + str(Ww) + ">") #NameError: name 'Ww' is not defined root.mainloop() SGA -Original Message- From: Python-list On Behalf Of MRAB via Python-list Sent: Saturday, February 24, 2024 7:49 PM To: python-list@python.org Subject: Re: Problem resizing a window and button placement On 2024-02-25 00:33, Steve GS via Python-list wrote: > "Well, yes, in Python a > variable created inside a > function or method is local to > that function unless you > declare it global." > > Yes, I knew that. I tried to > global it both before the > function call and within it. > Same for when I created the > variable. If I try to use it > in the rest of the code, it > keeps coming up as not > declared. In other functions, > I can 'return' the variable > but that apparently would not > work for this function. > > Is this type of function any > different that that which I > have been using? > Please post a short example that shows the problem. -- https://mail.python.org/mailma n/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem resizing a window and button placement
On 2024-02-25 00:33, Steve GS via Python-list wrote: "Well, yes, in Python a variable created inside a function or method is local to that function unless you declare it global." Yes, I knew that. I tried to global it both before the function call and within it. Same for when I created the variable. If I try to use it in the rest of the code, it keeps coming up as not declared. In other functions, I can 'return' the variable but that apparently would not work for this function. Is this type of function any different that that which I have been using? Please post a short example that shows the problem. -- https://mail.python.org/mailman/listinfo/python-list
RE: Problem resizing a window and button placement
"Well, yes, in Python a variable created inside a function or method is local to that function unless you declare it global." Yes, I knew that. I tried to global it both before the function call and within it. Same for when I created the variable. If I try to use it in the rest of the code, it keeps coming up as not declared. In other functions, I can 'return' the variable but that apparently would not work for this function. Is this type of function any different that that which I have been using? SGA -Original Message- From: Python-list On Behalf Of Thomas Passin via Python-list Sent: Saturday, February 24, 2024 8:40 AM To: python-list@python.org Subject: Re: Problem resizing a window and button placement On 2/24/2024 3:20 AM, Steve GS via Python-list wrote: > Yes, I ran that elegantly > simple code. The print > statement reports the X, Y, > Height and Width values. > However, I do not see how to > capture the width value. > > I experimented with the code > Vwidth = rootV.winfo_width() > and it also reports the width > as I resize the window. > > However, I cannot seem to use > the variable Vwidth outside > the sub routine. It is acting > as if Vwidth is not global but > I added that. It is reported > that Vwidth is not defined > when I try to use it in my > code. Well, yes, in Python a variable created inside a function or method is local to that function unless you declare it global. That characteristic is called its "scope". But if you think you need it to be a global variable you should rethink your design. For one thing, before the next time you use your global variable the window size may have changed again. Instead, it would be better to have the function that responds to the resize event perform the action that you want, or call another function that does, passing the new width to it. Note that in most programming languages, variables have a scope. The rules about those scopes vary between languages. > > So close.. > SGA > > -Original Message- > From: Barry > > Sent: Saturday, February 24, > 2024 3:04 AM > To: Steve GS > > Cc: MRAB > ; > python-list@python.org > Subject: Re: Problem resizing > a window and button placement > > > >> On 24 Feb 2024, at 04:36, > Steve GS via Python-list > > wrote: >> >> How do I extract the values >> from args? > > You can look up the args in > documentation. > You can run the example code > MRAB provided and see what is > printed to learn what is in > the args. > > Barry > > -- https://mail.python.org/mailma n/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem resizing a window and button placement
On 2024-02-24, MRAB via Python-list wrote: > On 2024-02-24 01:14, Steve GS via Python-list wrote: > >> Python, Tkinter: How do I determine if a window has been resized? I >> want to locate buttons vertically along the right border and need >> to know the new width. The buttons are to move with the change of >> location of the right-side border. > > Bind an event handler for '': > > 8< > [...] > 8< > > Are you placing the buttons yourself? I always use layouts and they > handle such things automatically. Yes, definitely what he said: use a layout manager. I hope this doesn't sound rude, but if you're calculating button positions based on window size, you're doing it wrong and will end up wasting a lot of time to produce something that won't work right. Use a layout manager: https://tkinterpython.top/layout/#:~:text=Tkinter%20has%20three%20built%2Din,%2C%20grid%20%2C%20and%20place%20managers. https://www.pythonguis.com/tutorials/create-ui-with-tkinter-pack-layout-manager/ https://www.pythonguis.com/tutorials/use-tkinter-to-design-gui-layout/ You'll have to spend a little time learning how they work, but in the end you'll get done sooner and have better results. -- Grant -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem resizing a window and button placement
On 2/24/2024 3:20 AM, Steve GS via Python-list wrote: Yes, I ran that elegantly simple code. The print statement reports the X, Y, Height and Width values. However, I do not see how to capture the width value. I experimented with the code Vwidth = rootV.winfo_width() and it also reports the width as I resize the window. However, I cannot seem to use the variable Vwidth outside the sub routine. It is acting as if Vwidth is not global but I added that. It is reported that Vwidth is not defined when I try to use it in my code. Well, yes, in Python a variable created inside a function or method is local to that function unless you declare it global. That characteristic is called its "scope". But if you think you need it to be a global variable you should rethink your design. For one thing, before the next time you use your global variable the window size may have changed again. Instead, it would be better to have the function that responds to the resize event perform the action that you want, or call another function that does, passing the new width to it. Note that in most programming languages, variables have a scope. The rules about those scopes vary between languages. So close.. SGA -Original Message- From: Barry Sent: Saturday, February 24, 2024 3:04 AM To: Steve GS Cc: MRAB ; python-list@python.org Subject: Re: Problem resizing a window and button placement On 24 Feb 2024, at 04:36, Steve GS via Python-list wrote: How do I extract the values from args? You can look up the args in documentation. You can run the example code MRAB provided and see what is printed to learn what is in the args. Barry -- https://mail.python.org/mailman/listinfo/python-list
RE: Problem resizing a window and button placement
Yes, I ran that elegantly simple code. The print statement reports the X, Y, Height and Width values. However, I do not see how to capture the width value. I experimented with the code Vwidth = rootV.winfo_width() and it also reports the width as I resize the window. However, I cannot seem to use the variable Vwidth outside the sub routine. It is acting as if Vwidth is not global but I added that. It is reported that Vwidth is not defined when I try to use it in my code. So close.. SGA -Original Message- From: Barry Sent: Saturday, February 24, 2024 3:04 AM To: Steve GS Cc: MRAB ; python-list@python.org Subject: Re: Problem resizing a window and button placement > On 24 Feb 2024, at 04:36, Steve GS via Python-list wrote: > > How do I extract the values > from args? You can look up the args in documentation. You can run the example code MRAB provided and see what is printed to learn what is in the args. Barry -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem resizing a window and button placement
> On 24 Feb 2024, at 04:36, Steve GS via Python-list > wrote: > > How do I extract the values > from args? You can look up the args in documentation. You can run the example code MRAB provided and see what is printed to learn what is in the args. Barry -- https://mail.python.org/mailman/listinfo/python-list