Cannot pass arguments from the tkinter widget.after function

前提是你 提交于 2019-11-29 14:31:59

I would try functools.partial to wrap your call as in:

widget.after(10, functools.partial(self.runBackup, mybackup))

Or you could define a local function that takes no arguments but passes the parameter (which is in essence what functools.partial does).

When you do this:

widget.after(10, self.runBackup(mybackup))

... You are telling Tkinter "run the command runBackup, and when it returns, use the result as an argument to after". Because runBackup returns None, the above is equivalent to:

self.runBackup(mybackup)
widget.after(10, None)

Instead, you want to give after a reference to the function, rather than calling the function. If the command needs arguments, those can be given to after as additional arguments.

For example:

widget.after(10, self.runBackup, mybackup)

Add: Using Lambda function format, the function doesn´t fail after several recursive calls. eg:

Function abc(par):
           stat-1
           stat-2
           ...
           stat-n
           root.after(1000, lambda : abc(par))

    ...

(it´s very useful to update a time-clock at left-top of window, for example...)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!