Last active
January 7, 2024 22:18
-
-
Save vigilantPotato/5a5f3b0d3ccb775b0148be66d5d390e7 to your computer and use it in GitHub Desktop.
example of moving button by dragging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tkinter | |
class MoveButtonByDragging(tkinter.Button): | |
""" | |
button which can move by dragging | |
""" | |
def __init__(self, root): | |
super().__init__( | |
root, | |
text="Move by Drag!", | |
bg="cyan", | |
fg="black", | |
) | |
self.bind("<B1-Motion>", self.move_button) | |
def move_button(self, event): | |
x = self.winfo_x() + event.x | |
y = self.winfo_y() + event.y | |
self.place(x=x, y=y) | |
def move_button2(self, event): | |
x = self.winfo_x() + event.x - self.winfo_width() / 2 | |
y = self.winfo_y() + event.y - self.winfo_height() / 2 | |
self.place(x=x, y=y) | |
if __name__ == "__main__": | |
root = tkinter.Tk() | |
b = MoveButtonByDragging(root) | |
b.place(x=0, y=0) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment