【问题标题】:How to center a tkinter widget in a sticky frame如何在粘性框架中居中 tkinter 小部件
【发布时间】:2015-01-10 04:39:41
【问题描述】:

我正在使用 tkinter 在 python3 中编写游戏,但在让网格执行我想要它执行的操作时遇到了一些麻烦。我已经浏览了至少五页的谷歌搜索结果,包括我能想到的如何提出这个问题的每个变体的堆栈溢出答案。我终于屈服并创建了这个帐户来询问这个问题。

我所拥有的:一个 button (newGameButton) 和一个位于 frame (topBar) 中心的 label (messageBox)自身居中但不水平跨越整个窗口(contentFrame)。

我设法得到的最好的(通过将sticky=W+E 放在 topBar 上):框架现在跨越整个窗口,按钮和标签保持相同的大小(标签上的粘性没有做任何事情,并且粘贴在该按钮仅使其与标签一样宽),现在卡在 topBar 的左侧。

我想要它做什么:让框架跨越整个窗口,标签也跨越整个窗口,并且按钮居中。

topBar 是columnspan=23 的原因是内容框架中的其余内容是 23 列宽(包括 0 列)。 我在框架中有按钮和标签的原因是我希望围绕它们的整个框具有边框效果。

代码:

self.contentFrame = Frame(self.root)
self.contentFrame.grid(row=0, column=0)
self.topBar = Frame(self.contentFrame, border=2, relief=RAISED)
self.topBar.grid(row=0, column=0, columnspan=23)
self.newGameButton = Button(self.topBar, text="New Game")
self.newGameButton.grid(row=0, column=0)
self.messageBox = Label(self.topBar, textvariable=self.message, height=2)
self.messageBox.grid(row=1, column=0, sticky=W+E)

有人有什么想法吗?我现在很绝望。

【问题讨论】:

    标签: python button tkinter grid centering


    【解决方案1】:

    问题是您的所有列都没有权重。决定哪些列(和行)获得任何额外空间的是权重属性。由于您的列都没有非零权重,因此没有为它们分配任何额外空间,因此它们保持尽可能小。

    根据经验,您应该始终为框架中的至少一行和一列赋予非零权重。在您的情况下,为所有帧赋予第 0 行和第 0 列的权重 1 似乎可行:

    self.root.grid_columnconfigure(0, weight=1)
    self.root.grid_rowconfigure(0, weight=1)
    self.contentFrame.grid_columnconfigure(0, weight=1)
    self.contentFrame.grid_rowconfigure(0, weight=1)
    self.topBar.grid_columnconfigure(0, weight=1)
    self.topBar.grid_rowconfigure(0, weight=1)
    

    【讨论】:

    • 有没有办法在一行中为多行或多列设置权重?或者将框架中的所有行和列设置为相同的权重?
    【解决方案2】:

    通过在谷歌中使用“如何使 tkinter 网格扩展”,我遇到了这个问题。

    布莱恩·奥克利的名言

    行和列具有“权重”,它描述了它们如何增长或缩小以填充主控中的额外空间。默认情况下,行或列的权重为零,这意味着您已告诉 >label 填充该列,但您没有告诉该列填充主框架。

    要解决此问题,请为列指定权重。

    class Test():
        def __init__(self,root):
            self.root = root
            self.root.columnconfigure(0, weight=1)
            self.root.config(bg='green')
            self.message = 'test message'
    
            self.contentFrame = Frame(self.root)
            self.contentFrame.config(background='black',borderwidth=5,relief ='sunken')
            self.contentFrame.grid(row=0, column=0, sticky='news')
            self.contentFrame.columnconfigure(0, weight=1)
    
            self.topBar = Frame(self.contentFrame, border=2, relief=RAISED)
            self.topBar.grid(row=0, column=0, columnspan=23,sticky=W+E)
            self.topBar.config(background='blue')
            self.topBar.columnconfigure(0, weight=1)
    
            self.newGameButton = Button(self.topBar, text="New Game")
            self.newGameButton.grid(row=0, column=0)
            self.newGameButton.config(background='red')
    
            self.messageBox = Label(self.topBar, text=self.message, height=2)
            self.messageBox.grid(row=1, column=0, columnspan=1,sticky=W+E)
            self.messageBox.config(background='yellow')
    
    Test(root)
    

    【讨论】:

    • W+E 和与 contentFrame 相同的列跨度使集中化工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 2011-10-20
    相关资源
    最近更新 更多