我想使用tkinter创建一个桌面应用程序。在标签中放置(大尺寸的)文本时,我总是会得到较大的垂直填充。我是否可以摆脱这个额外的空间?我想将文本放在标签的底部。
我已经尝试设置pady以及文本锚点。
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)
以下是其外观的图片:
我想删除文本下方(和上方)的绿色区域。
答案 0 :(得分:1)
使用Label
不能删除文本上方和下方的空格,因为高度对应于整数行,其高度由字体大小决定。该行高为低于基线的字母(如“ g”)保留了空间,但是由于您不使用此类字母,因此文本下方有很多空白空间(顶部没有多余的空间)在我的计算机上)。
要删除此空间,可以使用Canvas
代替Label
并将其大小调整为较小。
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()