Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Tkinter: Attempt to get widget size

I am trying to find the size of my window by using the winfo_geometry() function but it ends up returning 1x1+0+0 I have also tried winfo_height, winfo_width but i keep getting 1

CODE

from tkinter import *

root=Tk()

root.geometry('400x600')

print (root.winfo_width())
print (root.winfo_height())
print (root.winfo_geometry())

root.mainloop()
like image 321
Brandon Nadeau Avatar asked Nov 11 '12 00:11

Brandon Nadeau


1 Answers

You are trying to get the dimensions before the window has been rendered.

Add a root.update() before the prints and it shows the correct dimensions.

from Tkinter import *

root=Tk()

root.geometry('400x600')

root.update()

print (root.winfo_width())
print (root.winfo_height())
print (root.winfo_geometry())

root.mainloop()
like image 132
Tim Avatar answered Sep 21 '22 13:09

Tim