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('<Configure>',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 <learn2prog...@gmail.com>
Sent: Monday, February 26, 2024 4:04 AM
To: Steve GS <Gronicus@SGA.Ninja>; 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('<Configure>',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

Reply via email to