繁体   English   中英

居中窗口python tkinter

[英]Centering window python tkinter

我最近开始在 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))

好的,我已经找到并解决了问题。 借助 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

我认为这个答案并不完全在中心,可能会偏离一点,因为 h 返回 200 时它应该更少,但它看起来在中心并且工作正常。

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

一个示例程序:

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

希望有人会使用此代码,基本上这是一个解决方案,可用于将 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()

暂无
暂无

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

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