Python TKinter Set absolute Label size

846 Views Asked by At

I am looking to set the Label Widget width to an exact value - width=100 should be 100 pixels.

Is there a way to achieve this or should I be looking at using a different widget?

When using the TKinter Label, the width and height parameters refer to the text size - height=2 will set the label large enough for two lines of text, not 2 pixels as I would expect.

1

There are 1 best solutions below

6
On

You can assign a blank image to the label, then you can specify width option in pixels:

import tkinter as tk

root = tk.Tk()

blank = tk.PhotoImage()
tk.Label(root, image=blank, text="Hello", width=200, height=50, compound="c", bg="yellow", bd=0, padx=0).pack(padx=100, pady=10)
tk.Label(root, image=blank, text="World", width=200, height=30, compound="c", bg="cyan", bd=0, padx=0).pack(padx=100, pady=10)

root.mainloop()

Result:

enter image description here