Here is a small example:
from Tkinter import * root = Tk() class WindowDraggable(): def __init__(self, label): self.label = label label.bind('<ButtonPress-1>', self.StartMove) label.bind('<ButtonRelease-1>', self.StopMove) label.bind('<B1-Motion>', self.OnMotion) def StartMove(self, event): self.x = event.x self.y = event.y def StopMove(self, event): self.x = None self.y = None def OnMotion(self,event): x = (event.x_root - self.x - self.label.winfo_rootx() + self.label.winfo_rootx()) y = (event.y_root - self.y - self.label.winfo_rooty() + self.label.winfo_rooty()) root.geometry("+%s+%s" % (x, y)) label = Label(root, text='drag me') WindowDraggable(label) label.pack() root.mainloop()
This was almost correct for you, but you must compensate for the offset within the label itself. Please note that my example does not compensate for the border of the window. You will have to use certain tools to figure this out (so this example works fine when using overrideredirect(1) .
I assume that you come from a different programming language, so I will give you some tips while I am:
- Python does not end statements with
; (while valid syntax, there is no reason for this). - Method names must either
look_like_this sequentially or lookLikeThis . - Variables must not be declared. If you want to create an instance variable, do it in
__init__ (and definitely not outside the method unless you want a class variable).
source share