famansour

WEBCODES #Tkinter draggable

Sep 15th, 2020
942
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import tkinter as tk
  2.  
  3. class App(tk.Tk):
  4.     def __init__(self):
  5.         tk.Tk.__init__(self)
  6.         tk.Tk.withdraw(self) # ADDITINAL To hide main menu
  7.         self.floater = FloatingWindow(self)
  8.  
  9. class FloatingWindow(tk.Toplevel):
  10.     def __init__(self, *args, **kwargs):
  11.         tk.Toplevel.__init__(self, *args, **kwargs)
  12.         self.overrideredirect(True)
  13.         self.attributes( '-topmost', 1 ) # ADDITINAL topmost
  14.        
  15.         self.label = tk.Label(self, text="Click on the grip to move")
  16.         self.grip = tk.Label(self, bitmap="gray25")
  17.         self.grip.pack(side="left", fill="y")
  18.         self.label.pack(side="right", fill="both", expand=True)
  19.  
  20.         self.grip.bind("<ButtonPress-1>", self.start_move)
  21.         self.grip.bind("<ButtonRelease-1>", self.stop_move)
  22.         self.grip.bind("<B1-Motion>", self.do_move)
  23.  
  24.     def start_move(self, event):
  25.         self.x = event.x
  26.         self.y = event.y
  27.  
  28.     def stop_move(self, event):
  29.         self.x = None
  30.         self.y = None
  31.  
  32.     def do_move(self, event):
  33.         deltax = event.x - self.x
  34.         deltay = event.y - self.y
  35.         x = self.winfo_x() + deltax
  36.         y = self.winfo_y() + deltay
  37.         self.geometry(f"+{x}+{y}")
  38.  
  39. app=App()
  40. app.mainloop()
RAW Paste Data