Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot pass arguments from the tkinter widget.after function

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()
like image 337
Brad Conyers Avatar asked Jun 14 '12 19:06

Brad Conyers


People also ask

How do you pass an argument to an event handler in Tkinter?

The first >argument is the Event object passed to all event handlers, and the second and third >arguments will be set to their default values—the extra arguments we need to pass it. This technique can be extended to supply any number of additional arguments to >handlers.

What does mainloop() do in Python?

window.mainloop() tells Python to run the Tkinter event loop. This method listens for events, such as button clicks or keypresses, and blocks any code that comes after it from running until you close the window where you called the method.

How do you pass parameters in button click event in Python?

Method 1: Pass Arguments to Tkinter Button using the lambda function. Import the Tkinter package and create a root window. Give the root window a title(using title()) and dimension(using geometry()), now Create a button using (Button()).

What does Ttk do in Tkinter?

The tkinter. ttk module provides access to the Tk themed widget set, introduced in Tk 8.5. If Python has not been compiled against Tk 8.5, this module can still be accessed if Tile has been installed.


1 Answers

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)
like image 93
Bryan Oakley Avatar answered Nov 12 '22 04:11

Bryan Oakley