Reputation: 84
I wrote a code which prints window's size after it gets resized, but every time the function prints "200" for width and "200" for height, no matter the window size is. How can i solve the problem?
Here's the code:
from tkinter import *
Root=Tk()
Root.title("Window")
lab = Label(Root, text = "Test")
lab.pack()
Root.geometry("300x300")
RWidth=Root.winfo_reqwidth()
RHeight=Root.winfo_reqheight()
def check(event):
print(RWidth)
print(RHeight)
Root.bind("<Configure>",check)
mainloop()
Solved!
Upvotes: 2
Views: 2848
Reputation: 335
I know I am two years late to answer this, but the one who is still referring this post can be definitely helped by this answer.
In tkinter we are having 2 methods to update all the pending idle tasks, which are update() and update_idletasks(). Below code works as intended. I have used winfo_width() instead of using winfo_reqwidth(). Nothing wrong in using winfo_reqwidth() but still I prefer using winfo_width() over winfo_reqwidth().
So the idea over here is to update the idle pending task using one of the two methods available [update(), update_idletasks()]. When those tasks are updated then we can call our winfo_width() and winfo_height() methods to get accurate values.
from tkinter import *
Root=Tk()
Root.title("Window")
lab = Label(Root, text = "Test")
lab.pack()
Root.geometry("300x300")
Root.update_idletasks()
def check(event):
RWidth = Root.winfo_width()
RHeight = Root.winfo_height()
print(RWidth)
print(RHeight)
Root.bind("<Configure>",check)
mainloop()
Explanation of update() and update_idletasks():-
Update method processes all the pending idle tasks, unvisited events, calling functions, and callbacks. The method is applicable for updating and processing all the events or tasks such as redrawing widgets, geometry management, configuring the widget property, etc.
It also ensures that if the application has any pending tasks, then it will just update or refresh the value that affects the whole part of the application. Using update for a single pending task would be nasty, thus Tkinter also provides the update_idletasks() method. It only updates the idle pending task that is stable or not updating in the application for some reason. It calls all the events that are pending without processing any other events or callback.
The update() and update_idletask() methods are useful for processing any pending or idle tasks. However, the only difference between update() and update_idletasks() is that update() processes all the events present in the application, while update_idletasks() only processes those events which are not running or stable.
Upvotes: 2
Reputation: 1324
from tkinter import *
Root=Tk()
Root.title("Window")
lab = Label(Root, text = "Test")
lab.pack()
Root.geometry("300x300")
RWidth=Root.winfo_width()
RHeight=Root.winfo_height()
def check(event):
RWidth = Root.winfo_width()
RHeight = Root.winfo_height()
print(RWidth)
print(RHeight)
Root.bind("<Configure>",check)
mainloop()
you have to write RWidth = Root.winfo_width() RHeight = Root.winfo_height() at proper place.
Upvotes: 2
Reputation: 386030
The winfo_req_width
and winfo_req_height
methods returns the width and height requested by the widget -- essentially, the width
and height
parameters when you create the window [1]. The default requested width and height for the root window is 200 pixels.
When you set the geometry, that doesn't change the requested width and height of the window, only the actual width and height. That is because the geometry
command is giving a command to the window manager, rather than changing the configuration of the widget itself.
If you want winfo_reqwidth
and winfo_reqheight
to return 300, you need to configure the width
and height
parameters of the window (eg: root.configure(width=300, height=300)
)
If you want to get the actual width, you need to call winfo_width
and winfo_height
. Also, you should call those functions each time in your event handler since the value will change if the user resizes the window:
def check(event):
print(root.winfo_width())
print(root.winfo_height())
[1] most widgets allow you to specify the width and height when creating it (eg: Label(root, width=300, height=300)
). Oddly, you can't specify those parameters when creating the root window. You can, however, change them after it's created via the configure
method (eg: root.configure(width=300, height=300)
).
Upvotes: 3