【问题标题】:How can I centre the whole window in Python?如何在 Python 中将整个窗口居中?
【发布时间】:2023-03-26 13:15:01
【问题描述】:

我用完全相同的代码创建了两个窗口。 在第一个窗口中,文本居中,但在第二个窗口中不是。 我会很感激你的帮助! 谢谢

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

【问题讨论】:

  • 使用newheader.pack(),默认.pack()居中。
  • 对于网格,您只需定义列权重。

标签: python tkinter layout


【解决方案1】:

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

这是一个例子:

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

【讨论】:

  • 非常感谢您的帮助!
  • @MihaiRadoi 很高兴为您提供帮助。如果我的回答解决了您的问题,请勾选答案旁边的复选标记,让大家知道问题已解决。
猜你喜欢
  • 2012-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多