如何在Python中居中整个窗口?

问题描述 投票:1回答:1

我用相同的代码创建了两个窗口。在第一个窗口中,文本居中,但在第二个窗口中没有。非常感谢你的帮助!谢谢

    def order_page(self):
        newwindow = Tk()
        newwindow.title("Take an Order")
        newwindow.geometry('1920x1080')
        newheader = Label(newwindow,
            text="Hello",
            fg="Black",
            bg="Bisque",
            pady=5,
            font="Verdana 10 bold italic",
            width=100,
            height=3)
        newheader.grid()
        newwindow.mainloop()
python tkinter layout
1个回答
0
投票

使用网格时,如果要将窗口小部件居中,则需要定义要居中的区域的权重。

这是一个例子:

from tkinter import *


newwindow = Tk()
newwindow.title("Take an Order")
newwindow.geometry('1920x1080')
# column configure is used to define the weight of a specific column.
newwindow.columnconfigure(0, weight=1)
# if you want to also want the row to expand then use rowconfigure()
# newwindow.rowconfigure(0, weight=1)

newheader = Label(newwindow, text="Hello", fg="Black", bg="Bisque", font="Verdana 10 bold italic", width=100, height=3)
newheader.grid(row=0, column=0, pady=5)

newwindow.mainloop()

结果:

enter image description here

使用rowconfigure(0, weight=1)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.