RE: Problem resizing a window and button placement
>> The configuration event hasn't fired at the time you include the print statement in the handler's def block, and therefore the print function inside your handler hasn't invoked. It won't be invoked until you resize the window. Exactly >> There is no point (really?) to saving the width and height outside your on_configure() function, because outside that function you can't know if they have been changed. There could even have been a race condition where you use one but the other changes before you get around to using it. Aside from using it to resized the window, is there no way to know the last value of the change for use in the program? I could write the value to a label and read it back later in the process but that sounds to be klugy. >> It's better just to ask tk for the values whenever you need them, as you do inside your handler. How would that be done? SGA -Original Message- From: Python-list On Behalf Of Thomas Passin via Python-list Sent: Monday, February 26, 2024 8:34 AM To: python-list@python.org Subject: Re: Problem resizing a window and button placement On 2/26/2024 6:02 AM, Steve GS via Python-list wrote: > Although your code produces the value of Ww outside the function, I do not see how I can use the value of Ww unless I close the program. The configuration event hasn't fired at the time you include the print statement in the handler's def block, and therefore the print function inside your handler hasn't invoked. It won't be invoked until you resize the window. There is no point to saving the width and height outside your on_configure() function, because outside that function you can't know if they have been changed. There could even have been a race condition where you use one but the other changes before you get around to using it. It's better just to ask tk for the values whenever you need them, as you do inside your handler. > import tkinter as tk > > Ww = None # What does this do? Why not Integer? > WwZ = None # These could be integers, like 0, but that would not be the correct # window sizes at that point. The window is either not constructed or it # has some definite size that is not zero. > def on_configure(*args): > global Ww > global WwZ > Ww = root.winfo_width() > print("9 Ww Inside =<"+str(Ww)+">") # works > WwZ = Ww * 2 > print("11 WwZ Inside =<"+str(WwZ)+">") # works > return(Ww) #Can I use this? > > root = tk.Tk() > root.bind('',on_con figure) > print("15 Ww Inside1 = <"+str(Ww)+">") > #Ww2 = int(Ww) * 2 # fails > print("17 WwZ Inside2 = <"+str(WwZ)+">") > > root.mainloop() > > Ww2 = int(Ww) * 2 #Works but only after the program stops > print("21 Ww Outside2 = <"+str(WwZ)+">") # Can I have concentric > loops? > > > SGA > > -Original Message- > From: Alan Gauld > Sent: Monday, February 26, 2024 4:04 AM > To: Steve GS ; python-list@python.org > Subject: Re: RE: Problem resizing a window and button placement > > On 26/02/2024 07:56, Steve GS via Python-list wrote: > >> Then there is that discovery >> element: Why is my original >> idea not working? I still >> cannot pass the value back >> from the function. What is >> different about this function >> that others would have given >> me the value? > > There is nothing different, see the code below. > print() is a function like any other. > In this case it is called after you close the window, ie after mainloop() exits. > But any other function called inside > mainloop - eg any other event handler can also access it. > > For example, if you added a button: > > def printW(): print("Button Ww = ", Ww) > > bw = tk.Button(root, text="Print Width", command=printW) > bw.pack() > > You would be able to print the value on demand. > >>> import tkinter as tk >>> >>> Ww = None >>> >>> def on_configure(*args): >>> global Ww >>> Ww = root.winfo_width() >>> print("Ww Inside =<"+str(Ww)+">") >>> >>> root = tk.Tk() >>> root.bind('',on_con figure) >>> root.mainloop() >>> >>> print("Ww Outside = <"+str(Ww)+">") > > -- > Alan G > Author of the Learn to Program web site http://www.alan-g.me.uk/ > http://www.amazon.com/author/a lan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/a langauldphotos > > -- 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 2/26/2024 6:02 AM, Steve GS via Python-list wrote: Although your code produces the value of Ww outside the function, I do not see how I can use the value of Ww unless I close the program. The configuration event hasn't fired at the time you include the print statement in the handler's def block, and therefore the print function inside your handler hasn't invoked. It won't be invoked until you resize the window. There is no point to saving the width and height outside your on_configure() function, because outside that function you can't know if they have been changed. There could even have been a race condition where you use one but the other changes before you get around to using it. It's better just to ask tk for the values whenever you need them, as you do inside your handler. import tkinter as tk Ww = None # What does this do? Why not Integer? WwZ = None # These could be integers, like 0, but that would not be the correct # window sizes at that point. The window is either not constructed or it # has some definite size that is not zero. def on_configure(*args): global Ww global WwZ Ww = root.winfo_width() print("9 Ww Inside =<"+str(Ww)+">") # works WwZ = Ww * 2 print("11 WwZ Inside =<"+str(WwZ)+">") # works return(Ww) #Can I use this? root = tk.Tk() root.bind('',on_configure) print("15 Ww Inside1 = <"+str(Ww)+">") #Ww2 = int(Ww) * 2 # fails print("17 WwZ Inside2 = <"+str(WwZ)+">") root.mainloop() Ww2 = int(Ww) * 2 #Works but only after the program stops print("21 Ww Outside2 = <"+str(WwZ)+">") # Can I have concentric loops? SGA -Original Message- From: Alan Gauld Sent: Monday, February 26, 2024 4:04 AM To: Steve GS ; python-list@python.org Subject: Re: RE: Problem resizing a window and button placement On 26/02/2024 07:56, Steve GS via Python-list wrote: Then there is that discovery element: Why is my original idea not working? I still cannot pass the value back from the function. What is different about this function that others would have given me the value? There is nothing different, see the code below. print() is a function like any other. In this case it is called after you close the window, ie after mainloop() exits. But any other function called inside mainloop - eg any other event handler can also access it. For example, if you added a button: def printW(): print("Button Ww = ", Ww) bw = tk.Button(root, text="Print Width", command=printW) bw.pack() You would be able to print the value on demand. import tkinter as tk Ww = None def on_configure(*args): global Ww Ww = root.winfo_width() print("Ww Inside =<"+str(Ww)+">") root = tk.Tk() root.bind('',on_configure) root.mainloop() print("Ww Outside = <"+str(Ww)+">") -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos -- https://mail.python.org/mailman/listinfo/python-list
Re: RE: RE: Problem resizing a window and button placement
On 26/02/2024 11:02, Steve GS via Python-list wrote: > Although your code produces the value of Ww outside the function, > I do not see how I can use the value of Ww unless I close the program. You have to use a function that operates inside the mainloop. Thats the nature of event driven programs, all activity happens inside the mainloop except initialisation and cleanup. So you need to create an event handler as I described below. Here is the complete program including the button: ### import tkinter as tk Ww=None def on_configure(*args): global Ww Ww = root.winfo_width() print("Ww Inside = <" + str(Ww) + ">") def printW(): print("Button Ww = ", Ww) root = tk.Tk() bw = tk.Button(root, text="Print Width", command=printW) bw.pack() root.bind('', on_configure) root.mainloop() print("Ww Outside = <" + str(Ww) + ">") Notice that the button callback picks up the latest value of Ww. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos -- https://mail.python.org/mailman/listinfo/python-list
RE: RE: Problem resizing a window and button placement
Although your code produces the value of Ww outside the function, I do not see how I can use the value of Ww unless I close the program. import tkinter as tk Ww = None # What does this do? Why not Integer? WwZ = None def on_configure(*args): global Ww global WwZ Ww = root.winfo_width() print("9 Ww Inside =<"+str(Ww)+">") # works WwZ = Ww * 2 print("11 WwZ Inside =<"+str(WwZ)+">") # works return(Ww) #Can I use this? root = tk.Tk() root.bind('',on_configure) print("15 Ww Inside1 = <"+str(Ww)+">") #Ww2 = int(Ww) * 2 # fails print("17 WwZ Inside2 = <"+str(WwZ)+">") root.mainloop() Ww2 = int(Ww) * 2 #Works but only after the program stops print("21 Ww Outside2 = <"+str(WwZ)+">") # Can I have concentric loops? SGA -Original Message- From: Alan Gauld Sent: Monday, February 26, 2024 4:04 AM To: Steve GS ; python-list@python.org Subject: Re: RE: Problem resizing a window and button placement On 26/02/2024 07:56, Steve GS via Python-list wrote: > Then there is that discovery > element: Why is my original > idea not working? I still > cannot pass the value back > from the function. What is > different about this function > that others would have given > me the value? There is nothing different, see the code below. print() is a function like any other. In this case it is called after you close the window, ie after mainloop() exits. But any other function called inside mainloop - eg any other event handler can also access it. For example, if you added a button: def printW(): print("Button Ww = ", Ww) bw = tk.Button(root, text="Print Width", command=printW) bw.pack() You would be able to print the value on demand. >> import tkinter as tk >> >> Ww = None >> >> def on_configure(*args): >> global Ww >> Ww = root.winfo_width() >> print("Ww Inside =<"+str(Ww)+">") >> >> root = tk.Tk() >> root.bind('',on_configure) >> root.mainloop() >> >> print("Ww Outside = <"+str(Ww)+">") -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos -- https://mail.python.org/mailman/listinfo/python-list
Re: RE: Problem resizing a window and button placement
On 26/02/2024 07:56, Steve GS via Python-list wrote: > Then there is that discovery > element: Why is my original > idea not working? I still > cannot pass the value back > from the function. What is > different about this function > that others would have given > me the value? There is nothing different, see the code below. print() is a function like any other. In this case it is called after you close the window, ie after mainloop() exits. But any other function called inside mainloop - eg any other event handler can also access it. For example, if you added a button: def printW(): print("Button Ww = ", Ww) bw = tk.Button(root, text="Print Width", command=printW) bw.pack() You would be able to print the value on demand. >> import tkinter as tk >> >> Ww = None >> >> def on_configure(*args): >> global Ww >> Ww = root.winfo_width() >> print("Ww Inside =<"+str(Ww)+">") >> >> root = tk.Tk() >> root.bind('',on_configure) >> root.mainloop() >> >> print("Ww Outside = <"+str(Ww)+">") -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos -- https://mail.python.org/mailman/listinfo/python-list
RE: Problem resizing a window and button placement
Musta misstit I had thought of that before I started the discussion but figured it would take more code than it finally needed. I guess I was also variable-dependent thinking that I would need the result elsewhere in the code. So far, I see that I don't need the value. Then there is that discovery element: Why is my original idea not working? I still cannot pass the value back from the function. What is different about this function that others would have given me the value? SGA -Original Message- From: Python-list On Behalf Of Thomas Passin via Python-list Sent: Sunday, February 25, 2024 5:55 PM To: python-list@python.org Subject: Re: Problem resizing a window and button placement On 2/25/2024 4:19 PM, Steve GS via Python-list wrote: > SOLUTION FOUND! > > The fix was to write the code that uses the width value and to place it into the function itself. > Kluge? Maybe but it works. Right, just what I wrote earlier: "have the function that responds to the resize event perform the action that you want" > Mischief Managed. > > > As for the most recent suggestion, it fails for me: > > Traceback (most recent call last): >File "F:/___zInsulin Code A 08-02-23/WinPic/IOWw.pyw", line 14, in > print("Ww Outside = <" + str(Ww) > + ">") > TypeError: bad operand type for unary +: 'str' > > With the need to close the window, it adds an extra step and intervention to the program to use. I am not sure how this help[s. > > As a curio, it would be interesting to see how to use the value of a variable, created in the function used here, and make it available to the code outside the function. > > > > SGA > > -Original Message- > From: Alan Gauld > Sent: Sunday, February 25, 2024 12:44 PM > To: Steve GS ; python-list@python.org > Subject: Re: RE: Problem resizing a window and button placement > > On 25/02/2024 03:58, Steve GS via Python-list wrote: > import tkinter as tk > > Ww = None > > def on_configure(*args): > global Ww > Ww = root.winfo_width() > print("Ww Inside = <" + str(Ww) + ">") > > root = tk.Tk() > root.bind('', on_configure) > root.mainloop() > > print("Ww Outside = <" + str(Ww) > + ">") > > Produces: > Ww Inside = <200> > Ww Inside = <200> > Ww Inside = <205> > Ww Inside = <205> > Ww Inside = <206> > Ww Inside = <206> > Ww Outside = <206> > > HTH > -- 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
Ww Inside = <250> Ww Inside = <249> Ww Inside = <250> Ww Outside = <1770662408256on_configure> Here is my result... SGA -Original Message- From: Python-list On Behalf Of MRAB via Python-list Sent: Sunday, February 25, 2024 6:40 PM To: python-list@python.org Subject: Re: Problem resizing a window and button placement On 2024-02-25 21:19, Steve GS via Python-list wrote: > SOLUTION FOUND! > > The fix was to write the code that uses the width value and to place it into the function itself. > Kluge? Maybe but it works. > > Mischief Managed. > > > As for the most recent suggestion, it fails for me: > > Traceback (most recent call last): >File "F:/___zInsulin Code A 08-02-23/WinPic/IOWw.pyw", line 14, in > print("Ww Outside = <" + str(Ww) > + ">") > TypeError: bad operand type for unary +: 'str' > It fails because there's a mistake. It should be: print("Ww Outside = <" + str(Ww) + ">") > With the need to close the window, it adds an extra step and intervention to the program to use. I am not sure how this help[s. > > As a curio, it would be interesting to see how to use the value of a variable, created in the function used here, and make it available to the code outside the function. > [snip] -- https://mail.python.org/mailma n/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list