I want to be able to make a function constantly run in a tkinter
window.
For example, the code,
>>> import tkinter
>>> Window=tkinter.Tk()
>>> ThisCouldBeAnyObject=tkinter.Label(Window,text="Aligned right.",bg="#0000FF")
>>> thisCouldBeAnyObject=tkinter.Label(Window,text="Aligned right.",bg="#0000FF")
>>> def Function():
ThisCouldBeAnyObject.place(x=Window.winfo_width()-128,y=24)
ThisCouldBeAnyObject.config(text="To small!" if Window.winfo_width()<128 else "Window size: "+str(Window.winfo_width()))
thisCouldBeAnyObject.place(x=(Window.winfo_width()-128)//128*128,y=64)
creates a window, 2 labels and function that positions the labels according to the width of the window. (See edit history for more simple version of example code.)
My understanding is that mainloop
causes tkinter
to loop through a piece of code that checks if buttons are clicked, mouse motion is detected, etc. depending on the elements in the window. Could I make it so that in the mainloop
, my function keeps getting called without the need for these triggers, so that (in this example) the label moves as the user resizes the window?
So far, I've worked out that the code,
>>> Window.bind("<Configure>",func=lambda x:Function())
seems to work for the example, but it would be helpful to know how to avoid having to limit the execution of the function to when the window is configured.
I don't want to use
grid
, orpack
because I feel that I could do more withplace
and it will be good practice ready for other languages (that could be used for games development).
This is my first question, so thank you for any advice on asking questions.
Yes. I know that this might not be exactly how your supposed to use block quotes, but it was the only style of formatting I could find that provides the visual queue that a piece of text is a comment that you don't need to read to answer the question.