Python Tkinter居中窗口

3

最近我开始在Python中使用tkinter,但是我遇到了窗口居中的问题。我在这个网站上尝试了所有的提示,但无论我尝试什么方法,窗口总是出现在屏幕的中央一条线上。窗口上已经有了小部件,没有居中时它们也可以正常工作,但如果有人能帮我解决我的问题,我会非常感激。

这是我到目前为止尝试过的内容。

root = Tk()
root.title("Password")
root.resizable(FALSE,FALSE)

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

w = mainframe.winfo_width()
h = mainframe.winfo_height()
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
3个回答

3

在调用winfo_height()winfo_width()时,窗口还没有高度或宽度,因此您需要使用winfo_reqwidth()winfo_reqheight()

以下是一个示例程序:

from tkinter import Tk
from tkinter import ttk

root = Tk()

style = ttk.Style()
style.configure("BW.TLabel", foreground="black", background="white")

l1 = ttk.Label(text="This is the best label in the world", style="BW.TLabel")
l1.pack()

w = l1.winfo_reqwidth()
h = l1.winfo_reqheight()
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
print(w, h, x, y)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))

root.mainloop()

但是为了实现这一点,我需要使用一个小部件来配置req_height()和req_width(),但我的代码使用了许多小部件(原帖仅包含我认为重要的部分)。 - zih301
如果我使用根节点,高度会太大。 - zih301
我在笔记本电脑上使用双显示器,并使用此代码,窗口在两个屏幕之间有点居中。 - A.W.

3

好的,我已经找到并解决了问题。借鉴OregonTrail的解决方案后,我发现如果窗口的大小是正确的且您只想更改位置,那么您可以轻松地将窗口移动到中心,而无需设置根的大小。

w = root.winfo_reqwidth()
h = root.winfo_reqheight()
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
root.geometry('+%d+%d' % (x, y)) ## this part allows you to only change the location

我认为这个答案不完全正确,可能有一些偏差,因为在应该返回小于200的值时h返回了200。但是它看起来位于中心并且运行良好。


-1
希望有人能够使用这段代码,基本上这是一个解决方案,您可以使用它将TopLevel窗口相对于主(父)窗口居中。 可能不是最干净的解决方案,但它会完成工作。
from Tkinter import *

class PasswordDialog(Toplevel):

    def __init__(self, master=None):
        Toplevel.__init__(self, master)
        self.master = master

        self.title("Password")

        self.label_info = Label(self, text="You need to enter your password", pady=10)
        self.label_info.grid(row=0, column=0, columnspan=2, padx=20, pady=10, sticky="ew")
        self.label_pw = Label(self, text="Enter password:", pady=10)
        self.label_pw.grid(row=1, column=0, padx=(20, 2), sticky="e")
        self.entry = Entry(self, show="*")
        self.entry.bind("<KeyRelease-Return>", self.store_pass_event)
        self.entry.grid(row=1, column=1, padx=(2,20), sticky="w")
        self.button = Button(self, command=self.store_pass, text="Log in")
        self.button.grid(row=2, column=0, columnspan=2, pady=10)

        self.update()

        size = tuple(int(_) for _ in self.geometry().split('+')[0].split('x'))
        parent_offset = tuple(int(_) for _ in self.master.geometry().split('x')[1].split('+'))

        parent_width = self.master.winfo_width()
        parent_height = self.master.winfo_height()
        
        x = parent_width//2 - size[0]//2 + parent_offset[1]
        y = parent_height//2 - size[1]//2 + parent_offset[2]

        self.geometry("+%d+%d" % (x, y))

    def store_pass_event(self, event):
        self.store_pass()

    def store_pass(self):
        self.master.password = self.entry.get()
        self.destroy()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接