Skip to content

Instantly share code, notes, and snippets.

@nakagami
Last active November 4, 2023 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nakagami/b93521c7c369b4986e967f5fdeef1690 to your computer and use it in GitHub Desktop.
Save nakagami/b93521c7c369b4986e967f5fdeef1690 to your computer and use it in GitHub Desktop.
Example tkinter event and draw canvas
import tkinter
root = tkinter.Tk()
root.geometry("600x400")
canvas = tkinter.Canvas(root, width=600, height=400, bg="white")
canvas.pack()
def event_handler(event):
print(event)
if event.type == tkinter.EventType.ButtonRelease:
canvas.create_oval(event.x - 20, event.y - 20, event.x + 20, event.y + 20, fill="red", width=0)
for event_type in tkinter.EventType.__members__.keys():
# Ignore them because they occur frequently.
if event_type in ("Expose", "Motion"):
continue
event_seq= "<" + event_type + ">"
try:
canvas.bind_all(event_seq, event_handler)
#print(event_type)
except tkinter.TclError:
pass
#print("bind error:", event_type)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment