【问题标题】:Label width in tkintertkinter 中的标签宽度
【发布时间】:2013-04-28 02:46:37
【问题描述】:

我正在用 tkinter 编写一个应用程序,我试图在一个框架中放置几个​​标签......不幸的是,

windowTitle=Label(... width=100)

windowFrame=Frame(... width=100)

是非常不同的宽度...

到目前为止,我使用这个代码:

windowFrame=Frame(root,borderwidth=3,relief=SOLID,width=xres/2,height=yres/2)
windowFrame.place(x=xres/2-160,y=yres/2-80)
windowTitle=Label(windowFrame,background="#ffa0a0",text=title)
windowTitle.place(x=0,y=0)
windowContent=Label(windowFrame,text=content,justify="left")
windowContent.place(x=8,y=32)

...

#xres is screen width
#yres is screen height

由于某种原因,设置标签宽度没有正确设置宽度,或者没有使用像素作为测量单位......那么,有没有办法以适应长度的方式放置 windowTitle 小部件帧,还是以像素为单位设置标签宽度?

【问题讨论】:

  • 您到底为什么要使用 Place 几何管理器?使用包或网格。
  • 通常不需要指定标签的宽度。将其打包到框架中,让几何管理器处理它。调整框架大小。
  • 我确实需要将框架设置为特定位置并具有特定大小...是否可以使用w.pack() 方法来调整框架内的小部件的大小?
  • 您将一个框架放置在一个明确的大小/位置,并且在这个框架内您将一个标签放置一个明确的大小/位置? (你确定你需要那样做吗?)
  • 嗯。放置器应该让您指定标签小部件的绝对宽度。当您说“设置标签宽度未正确设置宽度”时,您是在告诉标签要设置什么宽度,还是告诉放置者将标签放置在什么宽度?

标签: python tkinter width label frame


【解决方案1】:

heightwidth 在标签包含文本时以文本单位​​strong> 定义标签的大小。 按照@Elchonon Edelson 的建议设置框架大小+一个小技巧:

from tkinter import *
root = Tk()

def make_label(master, x, y, h, w, *args, **kwargs):
    f = Frame(master, height=h, width=w)
    f.pack_propagate(0) # don't shrink
    f.place(x=x, y=y)
    label = Label(f, *args, **kwargs)
    label.pack(fill=BOTH, expand=1)
    return label

make_label(root, 10, 10, 10, 40, text='xxx', background='red')
make_label(root, 30, 40, 10, 30, text='xxx', background='blue')

root.mainloop()

【讨论】:

  • 不是,真的是我的想法......但是,fill=X 应该很好地为我服务......谢谢......
猜你喜欢
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-24
  • 1970-01-01
  • 2014-10-30
  • 2018-07-26
相关资源
最近更新 更多