无法从 tkinter widget.after 函数传递参数 [英] Cannot pass arguments from the tkinter widget.after function

查看:32
本文介绍了无法从 tkinter widget.after 函数传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 tkinter 构建的 GUI 的一部分有一个弹出窗口,上面写着程序正在运行,请稍候."然后在它完成后窗口消失.我正在使用 widget.after 命令打开窗口并运行该命令.但是,如果我传递我调用参数的函数,则永远不会出现弹出窗口.举个例子:

Part of the GUI I'm building using tkinter has a pop-up window that says "Please wait while the program is running." then after it finishes the window goes away. I'm using the widget.after command to open the window and run the command. However if I pass the function I call arguments then the pop up window never occurs. Here's an example:

def backupWindow
    self.restoreCB = Toplevel()

    message = "Please wait while backup runs"
    Label(self.restoreCB, text=message, padx=100, pady=20).pack()

    widget.after(10, self.runBackup)

def runBackup(self):
    <backup code>
    self.backupCB.destroy()

这运行良好并执行我想要的操作,在备份运行时弹出窗口,然后在备份后关闭窗口.但是,如果我像下面的代码一样从 widget.after 传递 and 参数,请稍候"消息永远不会出现.

This runs fine and does what I want it to do, the window pops up while the backup runs, then the window closes after the backup. However, If I pass the and argument from the widget.after like the code below, the "please wait" message never shows up.

def backupWindow
    self.restoreCB = Toplevel()

    message = "Please wait while backup runs"
    Label(self.restoreCB, text=message, padx=100, pady=20).pack()

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

def runBackup(self,mybackup):
    <backup code using mybackup>
    self.backupCB.destroy()

推荐答案

我会尝试使用 functools.partial 来包装您的电话:

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

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

或者你可以定义一个不带参数但传递参数的局部函数(这本质上就是 functools.partial 所做的).

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

这篇关于无法从 tkinter widget.after 函数传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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