繁体   English   中英

如何控制位置 tkinter window 被拖上去时 window 会居中

[英]How to control the location tkinter window will center itself when the window gets dragged up

我正在制作一个 gui,但遇到了一个问题,不知道如何正确地修改所有的小部件,所以我决定不允许任何调整大小。 现在,当我将 window 拖到顶部时,我只想让 window 自行居中,我为此查找了事件,我相信是这样,但现在 window 不断将自身重置为原始位置,每当我移动window。我只希望它在 window 仅被拖到顶部时重置

from tkinter import *


def move(e):  # 
    root.geometry("1270x725+0+0")


root = Tk()
root.geometry("1270x725+0+0")
root.resizable(False, False)
button = Button(root, command=move)
button.place(x=10, y=10)
Entry = Entry(root)
Entry.place(relx=.4, rely=.5)
root.bind('<Configure>', move)

root.mainloop()

您可以检查event.y是否小于某个值。 如果它更小,则重置 position。

这是一个例子:

from tkinter import *


def move(e):  
    if e.y < 5:
        
        sw, sh = root.winfo_screenwidth(), root.winfo_screenheight()
        root.geometry(f"1270x725+{(sw-1270)//2}+{(sh-725)//2}")


root = Tk()
root.geometry("1270x725+0+0")
root.resizable(False, False)

root.bind('<Configure>', move)

root.mainloop()

暂无
暂无

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

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