Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter is giving me a _tkinter.TclError: bad event type or keysym "button" when i try to run it

Tags:

python

tkinter

I was following along with a tutorial on Tkinter and i tried to run my program and it crashes on startup.

mainwindow = Tk()

def leftclick(event):
    print("left")

def middleclick(event):
    print("middle")

def rightclick(event):
    print("right")

frame = Frame(mainwindow, width=300, height=250)
frame.bind("<button-1>", leftclick)
frame.bind("<button-2>", middleclick)
frame.bind("<button-3>", rightclick)
frame.pack()

mainwindow.mainloop()

I have looked at my code and the code from the video and i cant seem to find anything different that would cause python to give me a error. I am not sure if it is because i am using a newer version(because the video itself was made back in 2014) or if its some mistype.

like image 411
Valstailei Avatar asked Sep 11 '25 16:09

Valstailei


1 Answers

The correct name for mouse click events are Button-1 and so on .....

from tkinter import  *
mainwindow = Tk()

def leftclick(event):
    print("left")

def middleclick(event):
    print("middle")

def rightclick(event):
    print("right")

frame = Frame(mainwindow, width=300, height=250)
frame.bind("<Button-1>", leftclick)
frame.bind("<Button-2>", middleclick)
frame.bind("<Button-3>", rightclick)

frame.focus_set()

frame.pack()


mainwindow.mainloop()
like image 72
asad_hussain Avatar answered Sep 13 '25 06:09

asad_hussain