famansour

Tips&Tricks #Draggabale windows works well

Sep 15th, 2020
1,084
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from tkinter import *
  2. root = Tk()
  3.  
  4. class WindowDraggable():
  5.  
  6.     def __init__(self, root):
  7.         self.root = root
  8.         root.bind('<ButtonPress-2>', self.StartMove)
  9.         root.bind('<ButtonRelease-2>', self.StopMove)
  10.         root.bind('<B2-Motion>', self.OnMotion)
  11.  
  12.     def StartMove(self, event):
  13.         self.x = event.x
  14.         self.y = event.y
  15.  
  16.     def StopMove(self, event):
  17.         self.x = None
  18.         self.y = None
  19.  
  20.     def OnMotion(self,event):
  21.         x = (event.x_root - self.x - self.root.winfo_rootx() + self.root.winfo_rootx())
  22.         y = (event.y_root - self.y - self.root.winfo_rooty() + self.root.winfo_rooty())
  23.         root.geometry("+%s+%s" % (x, y))
  24.  
  25. WindowDraggable(root)
  26.  
  27. root.mainloop()
RAW Paste Data