java label 大小_如何获得Label的真实当前大小?

我在根窗口中有一个 Frame (根窗口大小是通过 .geometry() 设置的),其中我有两个填充 Frame 的 Label . 我想知道 Label 的当前大小,以便调整其文本的字体大小 .

I tried .winfo_reqheight()但我无法理解返回的值 . 以下示例举例说明了我遇到的问题(三个问题以粗体显示):

import Tkinter as tk

root = tk.Tk()

root.geometry("200x200")

# top level frame, containing both labels below. Expands to fill root

f = tk.Frame(root)

f.pack(expand=True, fill=tk.BOTH)

# the condition for the two cases mentioned in the text below

if True:

text = ""

else:

text = "hello\nhello\nhello\nhello"

# two labels, the top one's txt will be changed

l1 = tk.Label(f, text=text, font=('Arial', 40), background="green")

l1.pack(expand=True, fill=tk.BOTH)

l2 = tk.Label(f, text="hello", font=('Arial', 15), background="blue")

l2.pack(expand=True, fill=tk.BOTH)

# just in case, thank you https://stackoverflow.com/users/2225682/falsetru

for i in [f, l1, l2]:

i.update_idletasks()

print("top label: reqheight = {0} height = {1}, bottom label: reqheight = {2} height = {3}".format(

l1.winfo_reqheight(), l1.winfo_height(),

l2.winfo_reqheight(), l2.winfo_height()

))

root.mainloop()

案例1:条件设置为True(空文本字符串)

rxT6T.png

和输出

top label: reqheight = 66 height = 1, bottom label: reqheight = 29 height = 1

所有小部件都设置为展开,所以 how come their total height is 66+29=95 while the window is 200 px high?

How can I get the height 的i)空,ii)填充两种方式和iii)扩展 Label - 我将保留作为参考(如果 Label 增长我将知道它不得超过该参考)?

案例2:条件为False(多行文本字符串)

Spbqg.png

top label: reqheight = 246 height = 1, bottom label: reqheight = 29 height = 1

Why has the top Label crushed the bottom one? 有没有一种机制可以说'expand as much as you can but be vary of the other widgets?'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值