Tkinter - 使用Keybind调用函数添加变量

时间:2021-12-01 23:17:58

When I call a method with a KeyBind in Tkinter, e.g. parent.bind('<Return>', self.login), when it calls self.login it appears to add a second variable, as if it were calling self.login(var).

当我在Tkinter中使用KeyBind调用方法时,例如parent.bind(' ',self.login),当它调用self.login时,它似乎添加了第二个变量,好像它正在调用self.login(var)。

Although it varies, when I had it print var, it printed <Tkinter.Event instance at 0x028C5558>. Now I'm guessing this is either the item it was called from, or the event that called it, but when I had it do it multiple times, it showed the same thing.

虽然它有所不同,但当我打印var时,它打印 。现在我猜这是要么调用它的项目,要么是调用它的事件,但是当我多次执行它时,它显示了同样的事情。 实例在0x028c5558>

The problem was simply solved by adding var='asdf' in all of the methods called by keybinds, but does this have an important purpose? Is there something I need to be putting in my methods? If else, what is it for?

通过在keybinds调用的所有方法中添加var ='asdf'简单地解决了这个问题,但是这有一个重要的目的吗?我需要在我的方法中添加一些东西吗?如果不是,它是为了什么?

2 个解决方案

#1


When a binding fires, it always passes an object representing the event that caused the callback to be called. This has information such as the x,y coordinate of the event, the key that was pressed, a timestamp for when the event occurred, etc.

当绑定触发时,它总是传递一个对象,该对象表示导致回调被调用的事件。这包含诸如事件的x,y坐标,按下的键,事件发生时间的时间戳等信息。

You don't always need this information, but it is always given to the callback. You need to add an event keyword argument:

您并不总是需要这些信息,但总是会给回调。您需要添加一个事件关键字参数:

def myCallback(event=None):
    <your code here>

...
someWidget.bind("<1>", myCallback)

#2


In the very useful Effbot Tkinter guide, it states the following (emphasis mine):

在非常有用的Effbot Tkinter指南中,它说明了以下内容(强调我的):

widget.bind(event, handler)

If an event matching the event description occurs in the widget, the given handler is called with an object describing the event.

如果在窗口小部件中发生与事件描述匹配的事件,则使用描述事件的对象调用给定的处理程序。

You see this as <Tkinter.Event instance at 0x...>. It has various attributes, describing what's happened (e.g. x and y location of the mouse pointer, the widget itself, etc.).

您将此视为 。它具有各种属性,描述发生了什么(例如鼠标指针的x和y位置,小部件本身等)。 实例位于0x>

#1


When a binding fires, it always passes an object representing the event that caused the callback to be called. This has information such as the x,y coordinate of the event, the key that was pressed, a timestamp for when the event occurred, etc.

当绑定触发时,它总是传递一个对象,该对象表示导致回调被调用的事件。这包含诸如事件的x,y坐标,按下的键,事件发生时间的时间戳等信息。

You don't always need this information, but it is always given to the callback. You need to add an event keyword argument:

您并不总是需要这些信息,但总是会给回调。您需要添加一个事件关键字参数:

def myCallback(event=None):
    <your code here>

...
someWidget.bind("<1>", myCallback)

#2


In the very useful Effbot Tkinter guide, it states the following (emphasis mine):

在非常有用的Effbot Tkinter指南中,它说明了以下内容(强调我的):

widget.bind(event, handler)

If an event matching the event description occurs in the widget, the given handler is called with an object describing the event.

如果在窗口小部件中发生与事件描述匹配的事件,则使用描述事件的对象调用给定的处理程序。

You see this as <Tkinter.Event instance at 0x...>. It has various attributes, describing what's happened (e.g. x and y location of the mouse pointer, the widget itself, etc.).

您将此视为 。它具有各种属性,描述发生了什么(例如鼠标指针的x和y位置,小部件本身等)。 实例位于0x>