【问题标题】:How do I change the position of a Label inside of a Frame? [duplicate]如何更改标签在框架内的位置? [复制]
【发布时间】:2015-09-30 22:34:18
【问题描述】:

我正在尝试放置我的标签,以便它们与框架的左侧对齐。但是,它们显示在中心,如下所示。我必须在配置中放置什么才能正确对齐它们?

这是我处理此问题的代码部分:

this.topFrame = Frame(this.chatWindow,relief="sunken",bd=1)
this.midFrame = Frame(this.chatWindow,relief="sunken",bd=1)
this.topFrame.pack(fill="x",expand = True)
this.midFrame.pack(fill="x",expand = True)

向热门用户框架添加海报

for poster in topPosters:
   label = Label(this.topFrame,text=poster)
   label.config(anchor=W,justify=LEFT)
   label.pack()

将消息添加到消息框架

for message in messageList:
   label = Label(this.midFrame,text=message)
   label.config(anchor=W,justify=LEFT)
   label.pack()

【问题讨论】:

    标签: python user-interface tkinter label frame


    【解决方案1】:

    将标签的anchor属性设置为"w"(西):

    label = Label(this.midFrame, text=message, anchor="w")
    

    另一个问题是您需要使用packfill 属性,以便标签填满整行。

    label.pack(side="top", fill="x")
    

    【讨论】:

    • 啊,谢谢!我知道我在那里放错了东西。
    • 嗯,很奇怪。我试过了,但它们仍然在中心。
    • @Shan:对不起。我忘了注意到您在打包时也没有使用fill 属性。我会更新答案。
    【解决方案2】:

    你可以试试grid,而不是pack

    label.grid(sticky = W)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-17
      • 2011-04-01
      • 1970-01-01
      • 2017-02-17
      • 2020-12-26
      • 1970-01-01
      • 2016-09-20
      • 2011-10-29
      相关资源
      最近更新 更多