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

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

问题描述

我使用Tkinter的建设GUI部分有说,一个弹出窗口的程序运行时,请稍候。随后它结束后,窗口消失。我使用的是widget.after命令打开窗口并运行命令。但是,如果我通过函数调用我的论点则永远不会发生的弹出窗口。这里有一个例子:

 高清备份窗口
    self.restoreCB = Toplevel为()    消息=请等待备份运行
    标签(self.restoreCB,文本=消息,padx = 100,pady = 20).pack()    widget.after(10,self.runBackup)高清runBackup(个体经营):
    <备份code>
    self.backupCB.destroy()

这运行正常,并做什么,我想要它做的窗口弹出而备份运行,则窗口中的备份后关闭。然而,如果我通过和论据来自像下面的code中的widget.after,请稍候的消息一直没有出现。

 高清备份窗口
    self.restoreCB = Toplevel为()    消息=请等待备份运行
    标签(self.restoreCB,文本=消息,padx = 100,pady = 20).pack()    widget.after(10,self.runBackup(为MyBackup))高清runBackup(个体经营,为MyBackup):
    <备份code。使用为MyBackup>
    self.backupCB.destroy()


解决方案

我会尝试functools.partial来包装你的电话为:

  widget.after(10,functools.partial(self.runBackup,为MyBackup))

或者你可以定义一个本地函数不带任何参数,但传递的参数(这在本质上是functools.partial做什么)。

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()

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()

解决方案

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).

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

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