Reputation: 479
I am looking to change where widgets are placed in a frame based on the width of the frame. I have a function that does this, but I would like it to run if the frame size changes. Right now, if I press a button, the widgets reorganize, but of course I am looking for this function to run when the frame is resized, not when the button is pressed.
The frame in this example is stuck to its parent window and filling it, so detecting either window resizing or frame resizing would be an acceptable trigger to run the function.
I have looked for events using intellisense and also done searches on the internet for some time but haven't found a solution.
Here is my code:
from tkinter import *
def reorganizeButtons():
# if the widths of the buttons exceed width of frame, stack vertically
# else stack horizontally
if button1.winfo_width()+button2.winfo_width()>frame.winfo_width():
button1.grid(row=0,column=0, sticky=NSEW)
button2.grid(row=1,column=0,sticky=NSEW)
else:
button1.grid(row=0,column=0,sticky=NSEW)
button2.grid(row=0,column=1,sticky=NSEW)
frame.update_idletasks()
print ()
print ("button1 width: ", button1.winfo_width())
print ("button2 width: ", button2.winfo_width())
print ("frame width: ", frame.winfo_width())
def main():
global root
global frame
global button1
global button2
root = Tk()
root.maxsize(400,200) # sets a limit on how big the frame can be
root.title("Enlarge Window, then Press a Button")
frame=Frame(root)
frame.grid(row=0, column=0, sticky=NSEW)
root.columnconfigure(0,weight=1)
root.rowconfigure(0,weight=1)
button1=Button(frame, text="---Button1---", command=reorganizeButtons)
button2=Button(frame, text="---Button2---", command=reorganizeButtons)
reorganizeButtons()
root.mainloop()
main()
Upvotes: 4
Views: 3179
Reputation: 479
Thank you for the advice TheLizzard, I found the answer based on your recommendation. Here is the final code, it works great. I am going to read up more on bindings to learn more. I used "root.bind("<Configure>", reorganizeButtons)"
to bind that event to the function I was trying to run. I also removed the button commands so they don't cause reorganization of the widgets. Here is the final code with the solution included.
from tkinter import *
def reorganizeButtons(event):
# if the widths of the buttons exceed width of frame, stack vertically
# else stack horizontally
if button1.winfo_width()+button2.winfo_width()>frame.winfo_width():
button1.grid(row=0,column=0, sticky=NSEW)
button2.grid(row=1,column=0,sticky=NSEW)
else:
button1.grid(row=0,column=0,sticky=NSEW)
button2.grid(row=0,column=1,sticky=NSEW)
frame.update_idletasks()
print ()
print ("button1 width: ", button1.winfo_width())
print ("button2 width: ", button2.winfo_width())
print ("frame width: ", frame.winfo_width())
def main():
global root
global frame
global button1
global button2
root = Tk()
root.maxsize(200,200) # sets a limit on how big the frame can be
root.title("Resize to Reorganize Buttons")
frame=Frame(root)
frame.grid(row=0, column=0, sticky=NSEW)
root.columnconfigure(0,weight=1)
root.rowconfigure(0,weight=1)
button1=Button(frame, text="---Button1---")
button2=Button(frame, text="---Button2---")
root.bind("<Configure>", reorganizeButtons)
root.mainloop()
main()
Upvotes: 6