繁体   English   中英

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

[英]How can I centre the whole window in Python?

我用相同的确切代码创建了两个窗口。 在第一个窗口中,文本居中,但是在第二个窗口中,居中。 多谢您的协助! 谢谢

    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()

当使用网格并且要居中放置小部件时,需要定义要居中区域的权重。

这是一个例子:

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()

结果:

在此处输入图片说明

使用rowconfigure(0, weight=1)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM