标记宽度在tkinter中

Mir*_*ac7 6 python label tkinter frame width

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

windowTitle=Label(... width=100)
Run Code Online (Sandbox Code Playgroud)

windowFrame=Frame(... width=100)
Run Code Online (Sandbox Code Playgroud)

宽度差异很大......

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

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
Run Code Online (Sandbox Code Playgroud)

出于某种原因,设置标签宽度不能正确设置宽度,或者不使用像素作为测量单位......那么,有没有办法以windowTitle适应框架长度的方式放置窗口小部件,或者以像素为单位设置标签宽度

kal*_*nik 10

heightwidth在包含文本时以文本单位定义标签的大小.关注@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()
Run Code Online (Sandbox Code Playgroud)