使用tkFont.Font,可以设置字体大小(以像素为单位),方法是将其设为负值:tkFont.Font(..., size=-20)
因此,如果从屏幕尺寸(以像素为单位)导出的每个标签都有一个可变高度,则可以将字体设置为该变量:
^{pr2}$
这不是一个完美的解决方案,因为除非你的字体是完美的正方形,否则像素高度通常会大于每个字符的像素宽度。也许一个固定宽度的字体有一个比例(例如,如果字体的像素大小是6,高度是6*2),你可以利用它,但我不认为你可以直接设置字体的高度。在
另一种方法是直接将标签的大小指定给像素大小,使用一种类似于给它一个空白图像的方法,让它将像素大小注册为宽度/高度值:root = Tk()
# derived width and height
width = 400
height = 40
# blank photo image
image = PhotoImage()
Label(root, image=image, compound='center', text='Hello World',
width=width, height=height, bg='white').pack()
mainloop()
或者,两者的结合:root = Tk()
# derived width and height
width = 400
height = 40
# add a font
font = tkFont.Font(size=-height)
# blank photo image
image = PhotoImage()
Label(root, image=image, compound='center', text='Hello World',
width=width, height=height, bg='white', font=font).pack()
mainloop()
希望我能正确地理解你想要达到的目标,这对我有帮助。在