这是我在这个简单的 GUI 中使用的代码:
def main():
DOCROOT_LABEL = 'Document root:'
PORT_LABEL = 'Listen on port:'
APPNAME = 'PIPlayD'
TEXT_HEIGHT = 1
MF_WIDTH = 400
MF_HEIGHT = 150
# Loading main window
win = Tkinter.Tk(baseName = APPNAME)
win.wm_title(APPNAME)
# Setting main frame
f = Tkinter.Frame(win, width = MF_WIDTH, height = MF_HEIGHT)
f.pack_propagate(False)
# Setting labels
label_DocRoot = Tkinter.Label(f,
width = len(DOCROOT_LABEL),
anchor = Tkinter.W,
justify = Tkinter.LEFT,
text = DOCROOT_LABEL)
label_Port = Tkinter.Label(f,
width = len(PORT_LABEL),
anchor = Tkinter.W,
justify = Tkinter.LEFT,
text = PORT_LABEL)
# Setting text areas
text_DR = Tkinter.Text(f, height = TEXT_HEIGHT)
text_P = Tkinter.Text(f, height = TEXT_HEIGHT)
f.pack()
label_DocRoot.pack()
text_DR.pack()
label_Port.pack()
text_P.pack()
win.mainloop()
我已将其设置为将字符串的长度作为其宽度,我已将其锚属性设置为 W 并将其向左对齐(尽管据我所知, justify 不是必需的,因为它控制如何它在发生回车时对齐)。
现在,我注意到如果我更改len(DOCROOT_LABEL
为MF_WIDTH
, 以匹配框架宽度,它会正确对齐。
那么,如何对齐宽度与框架内父宽度不匹配的小部件?