Problem resizing a window and button placement

Alan Gauld learn2program at gmail.com
Mon Feb 26 07:04:26 EST 2024


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('<Configure>', 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




More information about the Python-list mailing list