my_w
in all our examples below.
my_w = tk.Tk()
my_w.resizable(width=0,height=0) # size change Not allowed
We can only allow horizontally resizing the window.
my_w.resizable(width=1,height=0)
my_w.state('zoomed')
my_w.minsize(280, 180) # (minimum ) Width , ( minimum ) height
my_w.maxsize(320,220) # (maximum ) width , ( maximum) height
We have to update the data to integer by using int() while using any other type of variable.
width,height=400,350
my_w.minsize(int(width/3), height)
d=str(int(width/2))+"x"+str(int(height/2))
my_w.geometry(d)
In the script below some lines are commented to remove the restrictions. You can include them. import tkinter as tk
my_w = tk.Tk() # parent window
width,height=300,200
v_dim=str(width)+'x'+str(height)
my_w.geometry(v_dim) # Size of the window
#my_w.minsize(280, 180) # (minimum ) Width , ( minimum ) height
#my_w.maxsize(320,220) # (maximum ) width , ( maximum) height
my_w.title("www.plus2net.com") # Adding a title
def my_resize(condition):
global width , height
if(condition=='increase'):
width=width+10
height=height+10
elif(condition=='decrease'):
width=width-10
height=height-10
d=str(width)+"x"+str(height)
my_w.geometry(d) # update the new width and height
b1=tk.Button(my_w,text='zoom ++ ',command=lambda:my_resize('increase'))
b1.grid(row=0,column=0,padx=10,pady=10)
b2=tk.Button(my_w,text='zoom -- ',command=lambda:my_resize('decrease'))
b2.grid(row=0,column=1,padx=10,pady=10)
b3=tk.Button(my_w,text='Full Screen',command=lambda:my_w.state('zoomed'))
b3.grid(row=0,column=2,padx=10,pady=10)
my_w.mainloop() # Keep the window open
my_w.winfo_geometry()
: The string showing size and on-screen location.my_w.winfo_height()
: The current height of the window.( May not be updated)my_w.winfo_width()
: The current width of the window.( May not be updated)my_w.winfo_reqheight()
: The requested minimum height required. May change after geometry managermy_w.winfo_reqwidth()
: The requested minimum width required. May change after geometry managermy_w.winfo_screenheight()
: Screen height in pixel my_w.winfo_screenwidth()
: Screen width in pixel my_w.winfo_idletasks()
: to force the display to be updated before the application next idles import tkinter as tk
my_w = tk.Tk()
width,height=50,217
v_dim=str(width)+'x'+str(height)
my_w.geometry(v_dim)
#my_w.maxsize(300,220) # (maximum ) width , ( maximum) height
#my_w.minsize(250,220) # (minimum ) width , ( minimum) height
my_w.resizable(True, True)
my_w.title("www.plus2net.com") # Adding a title
languages = ("PHP",'Python','Perl','JQuery','Java','MySQL','CSS','Oracle')
var = 0
def my_upd():
print('****')
print('screenwidth',str(my_w.winfo_screenwidth())) # Width of the screen
print('reqwidth',str(my_w.winfo_reqwidth())) # Requested width of the window
print('width',str(my_w.winfo_width())) # width of the window
print('screenheight',str(my_w.winfo_screenheight())) # Screen height
print('reqheight:',str(my_w.winfo_reqheight())) # rquested width of the window
print('height:',str(my_w.winfo_height())) # width of the window
print('****')
for language in languages:
btn = tk.Button(my_w, text=language,command=lambda:my_upd())
btn.grid(row=1,column=var,padx=2,pady=10)
var += 1
width=50*len(languages) # set width based on number of buttons
v_dim=str(width)+'x'+str(height)
my_w.geometry(v_dim)
print('screenwidth',str(my_w.winfo_screenwidth())) # Width of the screen
print('reqwidth',str(my_w.winfo_reqwidth())) # Requested width of the window
print('width',str(my_w.winfo_width())) # width of the window
print('screenheight',str(my_w.winfo_screenheight())) # Screen height
print('reqheight:',str(my_w.winfo_reqheight())) # rquested width of the window
print('height:',str(my_w.winfo_height())) # width of the window
my_w.update_idletasks() # Update the idle to update display
print('####')
print('screenwidth',str(my_w.winfo_screenwidth())) # Width of the screen
print('reqwidth',str(my_w.winfo_reqwidth())) # Requested width of the window
print('width',str(my_w.winfo_width())) # width of the window
print('screenheight',str(my_w.winfo_screenheight())) # Screen height
print('reqheight:',str(my_w.winfo_reqheight())) # rquested width of the window
print('height:',str(my_w.winfo_height())) # width of the window
print('####')
my_w.mainloop()
Purpose
- Ensures immediate UI updates.
- Processes pending widget changes without handling new events.
- Prevents UI lag when updating widgets dynamically.
What It Does?
✔️ Forces geometry updates (resizing, layout changes).
✔️ Redraws widgets without waiting for mainloop().
✔️ Keeps UI elements responsive during long-running tasks.
When to Use It?
- Inside loops where widgets need real-time updates.
- Before blocking operations like time.sleep() to prevent UI freezing.
- When using threads, since background threads cannot directly update Tkinter widgets.
Key Difference Between update_idletasks() and update()
✔️ update_idletasks() → Processes only pending idle tasks (widget redraws, layout updates).
✔️ update() → Processes all events, including user inputs (can cause re-entrant issues if used inside event handlers).
Why root.update_idletasks() is Required Even with Threading?
- Tkinter’s main loop runs only in the main thread, meaning background threads cannot directly update widgets.
- Even if the task runs in a separate thread, Tkinter does not automatically refresh the UI.
- update_idletasks() forces immediate UI updates without waiting for the event loop.
Why Isn’t Threading Enough?
❌ Even with threading, Tkinter does not update widgets in real-time.
❌ The UI refresh happens only when the main thread regains control.
❌ This causes delayed UI updates or no visible changes until the thread finishes.
How update_idletasks() Helps?
✔️ Forces immediate processing of widget updates.
✔️ Prevents UI lag during long-running operations.
✔️ Ensures real-time updates for progress bars, status messages, and live UI changes.
Conclusion
Using update_idletasks() in Tkinter threading ensures that widgets update immediately, keeping the UI smooth and responsive even when running background tasks. It is an essential tool for real-time feedback in Tkinter applications. 🚀