Home > OS >  How to control the location tkinter window will center itself when the window gets dragged up
How to control the location tkinter window will center itself when the window gets dragged up

Time:10-02

I'm making a gui but ran into an issue with not knowing how to reform all the widget correctly so I decided to not allow any resizing. Now I just want the window to center itself when I drag the window to the top, and I looked up the event for this and I believe it is , but now the window keeps resetting itself to the original spot it pops up whenever I move the window. I only want it to reset when the window gets dragged to the top only

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

CodePudding user response:

You can check if the event.y is smaller than a certain value. If it is smaller then, reset the position.

Here is an example:

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