Python 如何使用 grid() 水平居中小部件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33046790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:39:37  来源:igfitidea点击:

How to horizontally center a widget using grid()?

pythontkinterwidgetgrid-layout

提问by L4undry

I am using grid()to place widgets in a tkinter window. I am trying to put a label on the horizontal center of a window and have it stay there, even if the window is resized. How could I go about doing this?

我正在使用grid()在 tkinter 窗口中放置小部件。我试图在窗口的水平中心放置一个标签并让它留在那里,即使窗口被调整大小。我怎么能去做这件事?

I don't want to use pack(), by the way. I would like to keep using grid().

pack()顺便说一下,我不想使用。我想继续使用grid().

回答by Bryan Oakley

There's no trick -- the widget is centered in the area allocated to it by default. Simply place a label in a cell without any stickyattributes and it will be centered.

没有什么技巧——小部件默认位于分配给它的区域的中心。只需在没有任何sticky属性的单元格中放置一个标签,它就会居中。

Now, the other question is, how to get the area it is allocated to be centered. That depends on many other factors, such as what other widgets are there, how they are arranged, etc.

现在,另一个问题是,如何使分配的区域居中。这取决于许多其他因素,例如还有哪些其他小部件,它们的排列方式等。

Here's a simple example showing a single centered label. It does this by making sure the row and column it is in takes up all extra space. Notice that the label stays centered no matter how big you make the window.

这是一个显示单个居中标签的简单示例。它通过确保它所在的行和列占用所有额外空间来做到这一点。请注意,无论窗口有多大,标签都保持居中。

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="This should be centered")
        label.grid(row=1, column=1)
        self.grid_rowconfigure(1, weight=1)
        self.grid_columnconfigure(1, weight=1)

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).grid(sticky="nsew")
    root.grid_rowconfigure(0, weight=1)
    root.grid_columnconfigure(0, weight=1)
    root.mainloop()

You can get a similar effect by giving a weight to all rows and columns exceptthe one with the label.

您可以通过为带有标签的行和列之外的所有行和列赋予权重来获得类似的效果。

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="This should be centered")
        label.grid(row=1, column=1)

        self.grid_rowconfigure(0, weight=1)
        self.grid_rowconfigure(2, weight=1)
        self.grid_columnconfigure(0, weight=1)
        self.grid_columnconfigure(2, weight=1)

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).grid(sticky="nsew")
    root.grid_rowconfigure(0, weight=1)
    root.grid_columnconfigure(0, weight=1)

    root.mainloop()

回答by scotty3785

There is nothing special required. A widget will be in the middle of it's parent automatically. What is required to to tell the parent to fill all available space.

没有什么特别的要求。小部件将自动位于其父级的中间。告诉父级填充所有可用空间需要什么。

from tkinter import *
root = Tk()
root.geometry("500x500+0+0")
frmMain = Frame(root,bg="blue")

startbutton = Button(frmMain, text="Start",height=1,width=4)
startbutton.grid()

#Configure the row/col of our frame and root window to be resizable and fill all available space
frmMain.grid(row=0, column=0, sticky="NESW")
frmMain.grid_rowconfigure(0, weight=1)
frmMain.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

root.mainloop()

This uses grid rather than pack to place the widgets and the grid is configured to fill the entire size of the window. The button will appear in the centre regardless of the size of the window.

这使用 grid 而不是 pack 来放置小部件,并且网格被配置为填充窗口的整个大小。无论窗口大小如何,该按钮都会出现在中央。