3

I want to create a desktop-application using tkinter. When placing text (of big size) in Labels, I always get a large vertical padding. Can I anyhow get rid of this additional space? I would like to place the text right on the bottom of the label.

I have already tried to set pady as well as the text anchor.

self.lbl_temp = Label(self.layout, text='20°C', font=('Calibri', 140), bg='green', fg='white', anchor=S)
self.lbl_temp.grid(row=0, column=1, sticky=S)

Here is an image of how it looks:

screnshot

I would like to remove the green space below (and on top of) the text.

j_4321
  • 15,431
  • 3
  • 34
  • 61
Georg Edlbauer
  • 205
  • 2
  • 12
  • I think this is because the label reserves space for letters going below the baseline, like 'g', since you use a large font size, this extra space is large but if you put a 'g' after your text, you will see that there is not much space below it. – j_4321 Aug 13 '19 at 16:13

1 Answers1

1

Removing the space above and below the text cannot be done with a Label because the height corresponds to an integer number of lines whose height is determined by the font size. This line height reserves space for letters going below the baseline, like 'g', but since you don't use such letters, you have a lot of empty space below your text (I don't have as much extra space a the top on my computer though).

To remove this space, you can use a Canvas instead of a Label and resize it to be smaller.

import tkinter as tk

root = tk.Tk()

canvas = tk.Canvas(root, bg='green')
canvas.grid()
txtid = canvas.create_text(0, -15, text='20°C', fill='white', font=('Calibri', 140), anchor='nw')  
# I used a negative y coordinate to reduce the top space since the `Canvas` 
# is displaying only the positive y coordinates
bbox = canvas.bbox(txtid)  # get text bounding box
canvas.configure(width=bbox[2], height=bbox[3] - 40)  # reduce the height to cut the extra bottom space

root.mainloop()

result

j_4321
  • 15,431
  • 3
  • 34
  • 61