Search code examples
pythontkintersizemeasurement

Python Tkinter Window and Widget Size Measurements


Does anyone know in what units are the measurements made in tkinter objects?

For example:

ws='400' #i am assuming these are in pixels
hs='400'
root=tk.Tk()
root.geometry(ws+'x'+hs)

...

self.w1=tk.Label(self.parent,width=int(int(ws)*5/8) #The width here probably isn't in pixels
self.w2=tk.Button(self.parent,width=int(int(ws)*3/8)

If the width in the widgets Label and Button were also measured in pixels, this should fit them nicely within the window, regardless of its width. But it doesn't. Instead, i experimented and found that dividing the width by 10 for one widget and 25 for the other does the trick. So in what units is either of these measured?


Solution

  • It depends on the widget, and also on how the widget is configured. For Label and Button widgets the width and height refer to a number of average sized characters (internally, I believe it uses the width and height of the character zero). If you add an image to the label or button, the width refers to a number of pixels.

    You will usually get the best results by avoiding hard-coding pixel values. Tkinter is really good about computing the appropriate size for widgets, and then fitting them into the window.