Jakub Bláha
Jakub Bláha

Reputation: 1609

Get tkinter widget size in pixels

If I define for example tkinter.Button widget with parameters (width=10, height=1)(in characters) and then I want to retrieve it's size in pixels, how do I do it?

EDIT: I tried widget.winfo_height() and widget.geometry(), but all these functions return height defined in number of characters. I think it would be possible to create the same widget in a frame and then write frame.winfo_height() which would return size in pixels, but this is not so elegant solution.

Upvotes: 1

Views: 8957

Answers (2)

Kjell
Kjell

Reputation: 22

Normally I do get the correct size when following the above procedures, but the results from this example really puzzles me: from tkinter import *

root=Tk()

labelName = Label(root, text='label')
entryName = Entry(root, foreground='red')

labelName.update_idletasks()
entryName.update_idletasks()
wl = labelName.winfo_width()
we = entryName.winfo_width()
print('wl, we: ', we, wl)

wl = labelName.winfo_reqwidth()
we = entryName.winfo_reqwidth()

print('wl, we: ', we, wl)

entryName.insert(0, 'entry.................sajdALSKJDASKDjsc')

labelName.grid(row=0, column=0)
entryName.grid(row=0, column=1)

mainloop() 

outputs ???:

wl, we: 1 1
wl, we: 124 31

Upvotes: 2

Jakub Bláha
Jakub Bláha

Reputation: 1609

Ok, I figured it out. We must call widget.update() first before calling widget.winfo_height().

Upvotes: 11

Related Questions