键盘快捷方式“采用1个位置自变量但给了2个位置自变量". [英] Keyboard Shortcut "Takes 1 positional argument but 2 were given"

查看:87
本文介绍了键盘快捷方式“采用1个位置自变量但给了2个位置自变量".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试制作键盘快捷键以重置游戏.之前在__init__类中,我创建了一个菜单来启动新游戏,并使用self.reset重置得分/网格等. 我现在一直在尝试使用相同的命令来实现快捷方式-重置是该类中的一种方法.

Trying to make a keyboard shortcut to reset a game. Earlier in the __init__ class I create a menu to start a new game, and use self.reset to reset the score/grid etc. I now have been trying to implement the shortcut with the same command - the reset being a method within the class.

self._master.bind_all('<Control-n>', self.reset)

这是错误:

TypeError: reset() takes 1 positional argument but 2 were given

我的困惑是self.reset在__init__之前可以正常工作,但不适用于快捷方式吗?

My confusion is that the self.reset works fine earlier in the __init__ but then does not work for the shortcut?

我什至看不到快捷方式如何为该方法提供任何位置参数.

I do not see how the shortcut is even giving any positional arguments to the method.

现在,如果将其更改为self.reset(),则会收到有关当前类缺少_game属性的错误消息.

Now if I change it to self.reset() I get an error about the current class missing the _game attribute.

这是重置方法:

def reset(self):
    self._game.get_default_score()
    self._game.reset()
    self._grid_view.draw(self._game.grid, self._game.find_connections())

推荐答案

这很容易-不管绑定快捷方式的机制是什么,它都会将一个额外的参数传递给您的reset方法.

That is easy - whatever the mechanism that binds the shortcut does, it is passing an extra parameter to your reset method.

由于您根本不在乎它是什么,所以只需声明您的方法即可接受 一个额外的可选参数,您应该会很好:

Since you don't care what it is at all, just declare your method to accept an extra optional parameter, and you should be good:

...
def reset(self, event=None):
    self._game.get_default_score()
    ...

因此,在搜索"bind_all"时,我们发现您的代码实际上使用了tkinter,而tkinter传递给您的方法就是"event"(事件),该对象具有有关实际按下了哪些键的信息,例如- http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

So, searching for "bind_all" we find out your code actually uses tkinter, and what tkinter passe along to your method is the "event" - an object with information on what key whas actually pressed and such - http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

这篇关于键盘快捷方式“采用1个位置自变量但给了2个位置自变量".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆