Python函数抛出TypeError,说我输入了一个参数,而我显然没有输入

我有点麻烦了。我正在尝试将函数绑定到 Python 的 tkinter 模块中的鼠标单击。每次单击时,该函数都会为变量加 1。该函数不带参数。但是,当我将该功能绑定到鼠标单击时,它说:


TypeError: func() takes 0 positional arguments but 1 was given.

我从未给函数提供任何参数,只是将其绑定到鼠标单击,Python 仍然认为我做错了什么。帮助?代码附后。


from tkinter import *

root = Tk()

root.geometry('800x600')

root.title("Sim Game")

c = Canvas(root, height=400, width=600, bg='red')

x = 0

def func():

    global x

    x += 1

c.bind("<Button-1>", func)


c.pack()


root.mainloop()


慕妹3146593
浏览 38回答 1
1回答

蝴蝶刀刀

您有几个选择:def&nbsp;func(event=None)或者c.bind('<Button-1>',&nbsp;lambda&nbsp;e:&nbsp;func())
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python