5

I am trying to make a tkinter label containing text with width and height defined by pixel dimensions. It is made clear that a label widget containing text's size is defined in terms of its text size in the docs:

If the label displays text, the size is given in text units. If the label displays an image, the size is given in pixels (or screen units). If the size is set to 0, or omitted, it is calculated based on the label contents.

I have tried using this information to achieve what I am trying to do. I want to create labels of fixed width 700 and variable height as I am creating a timetable application. Here's an example of what I have tried so far:

import tkinter as tk

root = tk.Tk()
root.geometry("800x600")

height = 20

label = tk.Label(root,width=700,bg="white",text="test",borderwidth=0,font=("Calibri",height))
label.place(x=10,y=10)

root.mainloop()

This almost achieves what I want in terms of height, but I would expect the height to be 1 pixel when height = 1, but it is actually 5 pixels (I've got very good eyesight!). As for width, its completely off the screen as its 700 times the width of a character.

Using individual frames as each "label" and then creating label widgets as children of these frames does not work either, as the frame just gets resized to fit the label. My question is: is there a way to create a text-containing label sized by pixel dimensions?

Rob Murray
  • 1,773
  • 6
  • 20
  • 32

2 Answers2

10

I have found a solution in my case. I used a frame, disabled its pack_propagate and made a label the child of this frame. I hope others will find it useful. This question helped me a lot.

import tkinter as tk

root = tk.Tk()
root.geometry("800x600")

height = 20

label_frame = tk.Frame(root,width=700,height=height,bg="white")
label_frame.pack_propagate(0) # Stops child widgets of label_frame from resizing it
tk.Label(label_frame,bg="white",fg="black",text="test",font=("Calibri",15)).pack()
label_frame.place(x=10,y=10)

root.mainloop()
Community
  • 1
  • 1
Rob Murray
  • 1,773
  • 6
  • 20
  • 32
3

doing a timetable is something that lends itself to using the grid packing method, which has a grid_columnconfigure and a grid_rowconfigure method which can be used to set the minimum and maximum size of a row or column, and the rate at which they expand:

import tkinter as tk

root = tk.Tk()

tk.Label(root, text="Test").grid(column=1, row=1, sticky="nesw")

root.grid_columnconfigure(1, minsize=700)

root.mainloop()
James Kent
  • 5,763
  • 26
  • 50
  • This doesn't really answer my question. I *have* to use `place` due to the layout of the timetable and the question is not concerned with the use of grid, so my question still stands. – Rob Murray Feb 15 '16 at 14:33
  • Well maybe I don't *have* to use place, but it's certainly preferable. This still doesn't answer my question, however. – Rob Murray Feb 15 '16 at 15:27
  • 1
    can you explain why it's preferable, knowing your reasons may help suggest a workaround... – James Kent Feb 15 '16 at 15:51
  • I answered my own question, thanks for your suggestion but my answer has worked a lot better for me. – Rob Murray Feb 15 '16 at 17:31