python - 如何在 python 中触发和监听事件?

标签 python tkinter

如何在 python 中触发和监听事件?

我需要一个实际的例子..

谢谢

更新

#add Isosurface button
def addIso():
    #trigger event

self.addButton = tk.Button(self.leftFrame, text="Add", command=addIso) #.grid(column=3, row=1)
self.addButton.pack(in_=self.leftFrame, side="right", pady=2)

最佳答案

TKinter 对象有一个绑定(bind)函数,它接受两个参数:第一个是一个字符串,表示您要监听的事件名称(在本例中可能是“”),第二个是一个接受触发事件的方法。事件作为参数。

示例:

def addButton_click(event):
    print 'button clicked'
self.addButton.bind("<Button-1>", addButton_click)

我相信即使从另一个类使用它仍然有效:

class X:
    def addButton_click(self, event):
        print 'button clicked'
...
inst = X()
self.addButton.bind("<Button-1>", inst.addButton_click)

关于python - 如何在 python 中触发和监听事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3969947/

相关文章:

python - 对于命令行应用程序,什么是 python 替代 thor (ruby)?

python - 将静态方法动态添加到 Python 类

python - 在 Matplotlib 图形窗口中更改图标

python - Tkinter Canvas 移动留下像素痕迹

python - 对整个文档进行语义搜索的正确方法?

python - 未找到 Psycopg2 图像

python - 在二维张量上滚动具有可变步长值的行

python - 如何在执行我们通过 pyinstaller 生成的可执行文件时隐藏黑屏?

python pack() 和 grid() 方法在一起

python - 如何链接在 for 循环中创建的 python tkinter 小部件?