tkinter python 模块中的绑定变量
Variable in bind in tkinter python module
我想知道是否有任何方法可以在 tkinter 的绑定中使用的回调函数中使用全局变量。
我指的是:
canvas = Canvas(root, width=500, height=500)
canvas.bind('<B1-Motion>', func)
其中 func
现在是拖动鼠标时触发的一些函数。我想要的是这样的:
canvas.bind('<B1-Motion>', func(arg))
结合:
def func(event, arg):
commands
我可以从 https://docs.python.org/3/library/tkinter.html 中看到,一个参数,即事件本身,被赋予了回调函数,但是不给这个方法任何方式来修改它的回调似乎是在浪费潜力另一种方式。
也许我弄错了,有一些技术原因导致这通常是不可能的,或者也许有替代绑定的方法。
我基本上期待的是:
buttoname = Button(...,...,..., command = Lambda: func(arg))
如有指点,将不胜感激。
问候
使用 lambda
接收事件参数并将其传递。
canvas.bind('<B1-Motion>', lambda e: func(e, arg))
我想知道是否有任何方法可以在 tkinter 的绑定中使用的回调函数中使用全局变量。
我指的是:
canvas = Canvas(root, width=500, height=500)
canvas.bind('<B1-Motion>', func)
其中 func
现在是拖动鼠标时触发的一些函数。我想要的是这样的:
canvas.bind('<B1-Motion>', func(arg))
结合:
def func(event, arg):
commands
我可以从 https://docs.python.org/3/library/tkinter.html 中看到,一个参数,即事件本身,被赋予了回调函数,但是不给这个方法任何方式来修改它的回调似乎是在浪费潜力另一种方式。
也许我弄错了,有一些技术原因导致这通常是不可能的,或者也许有替代绑定的方法。
我基本上期待的是:
buttoname = Button(...,...,..., command = Lambda: func(arg))
如有指点,将不胜感激。
问候
使用 lambda
接收事件参数并将其传递。
canvas.bind('<B1-Motion>', lambda e: func(e, arg))