当我尝试运行 Tkinter 时,它给了我一个 _tkinter.TclError: bad event type or keysym "button"

Val*_*lei 4 python tkinter

我正在按照 Tkinter 的教程进行操作,并尝试运行我的程序,但它在启动时崩溃了。

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()
Run Code Online (Sandbox Code Playgroud)

我查看了我的代码和视频中的代码,我似乎找不到任何不同的东西会导致 python 给我一个错误。我不确定是否是因为我使用的是较新的版本(因为视频本身是在 2014 年制作的),或者是否存在拼写错误。

a87*_*874 6

鼠标单击事件的正确名称是 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()
Run Code Online (Sandbox Code Playgroud)