我正在尝试在一个主文件中运行不同的文件...因此,当我单击"p"时,我应该能够打开我的另一个文件并在其中播放......但是当我这样做时,总是会出现错误消息...
def play(self):
with open("TheUltimatePONG.py", "r") as the_file:
self._outputArea.insert("1.0", the_file.read())
wn.onkey(play, "p")
wn.listen()
出现一个错误说:
TypeError: play() missing 1 required positional argument: 'self'
我不明白这是什么意思。
错误消息非常明确:
TypeError: play() missing 1 required positional argument: 'self'
意味着您的 play()
函数被定义为需要self
参数,但在实际调用时不会传递一个参数。
如果wn.onkey(play, "p")
建立了一个没有参数的调用play()
的回调,那么不要定义它来期望一个self
参数!
也就是说,改变:
# This takes a "self" argument, which usually only makes sense for methods, not functions.
def play(self):
自:
# This version does not expect any arguments at all.
def play():