Tkinter Dragable Screen

    # Tkinter Dragable Screen



    import tkinter as tk

    def start_drag(event):
        root.x = event.x
        root.y = event.y

    def stop_drag(event):
        del root.x
        del root.y

    def drag(event):
        try:
            deltax = event.x - root.x
            deltay = event.y - root.y
            x = root.winfo_x() + deltax
            y = root.winfo_y() + deltay
            root.geometry(f"+{x}+{y}")
        except AttributeError:
            pass

    root = tk.Tk()
    root.geometry("400x300")

    # Create a button to close the window
    close_button = tk.Button(root, text="Close", command=root.destroy)
    close_button.pack(pady=20)

    # Make the window draggable
    root.bind("<ButtonPress-1>", start_drag)
    root.bind("<ButtonRelease-1>", stop_drag)
    root.bind("<B1-Motion>", drag)

    root.mainloop()

Comments

Popular posts from this blog

Express Production Setup - 4 | HELMET

Express Production Setup - 3 | CORS

Ensuring File Creation in the Current Directory when Converting Python (.py) Script to Executable (.exe).