move window with overrideredirect 1

move window with overrideredirect

import tkinter as tk


class Win(tk.Tk):
    def __init__(self):
        super().__init__()
        super().overrideredirect(True)
        self._offsetx = 0
        self._offsety = 0
        super().bind("" ,self.clickwin)
        super().bind("", self.dragwin)

    def dragwin(self,event):
        x = super().winfo_pointerx() - self._offsetx
        y = super().winfo_pointery() - self._offsety
        super().geometry(f"+{x}+{y}")

    def clickwin(self,event):
        self._offsetx = super().winfo_pointerx() - super().winfo_rootx()
        self._offsety = super().winfo_pointery() - super().winfo_rooty()


root = Win()

label_1 = tk.Label(root, text="Label 1")
label_1.pack(side="left")

label_2 = tk.Label(root, text="Label 2")
label_2.pack(side="left")

root.mainloop()

Here is what the above code is Doing:
1. We create a class called Win that inherits from tk.Tk.
2. We override the __init__ method of the parent class.
3. We call the overrideredirect method of the parent class. This method removes the window decorations.
4. We create two variables to store the x and y coordinates of the mouse pointer.
5. We bind the event to the clickwin method.
6. We bind the event to the dragwin method.
7. We create the dragwin method. This method gets the x and y coordinates of the mouse pointer and sets the geometry of the window to those coordinates.
8. We create the clickwin method. This method gets the x and y coordinates of the mouse pointer and stores them in the _offsetx and _offsety variables.
9. We create an instance of the Win class and call its mainloop method.
10. We create two labels and pack them into the window.

Similar Posts