views:

755

answers:

2

I'm using tkinter with Python to create a user interface for a program that converts Excel files to CSV.

I created a label to act as a status bar, and set statusBarText as a StringVar() as the textvariable. inputFileEntry and outputFileEntry are textvariables that contain the input and output file paths.

def convertButtonClick():
    statusBarText.set('Converting...')

    if inputFileEntry.get() == '' or outputFileEntry.get() == '':
     statusBarText.set('Invalid Parameters.')
     return

    retcode = subprocess.('Program.exe' ,shell=true)

    if retcode == 0:
     statusBarText.set('Conversion Successful!')
    else:
     statusBarText.set('Conversion Failed!')

This function gets called when you click the convert button, and everything is working fine EXCEPT that the status bar never changes to say 'Converting...'.

The status bar text will get changed to invalid parameters if either the input or output are empty, and it will change to success or failure depending on the return code. The problem is it never changes to 'Converting...'

I've copied and pasted that exact line into the if statements and it works fine, but for some reason it just never changes before the subprocess runs when its at the top of the function. Any help would be greatly appreciated.

+1  A: 

How are you creating your Label? I have this little test setup:

from Tkinter import *
class LabelTest:

    def __init__(self, master):
        self.test = StringVar()

        self.button = Button(master, text="Change Label", command=self.change)
        self.button.grid(row=0, column=0, sticky=W)

        self.test.set("spam")
        self.testlabel = Label(master, textvariable = self.test).grid(row = 0,column = 1)
    def change(self):

        self.test.set("eggs")



root = Tk()
root.title("Label tester")
calc = LabelTest(root)

root.mainloop()

And it works. Did you make sure to use "textvariable = StatusBarText" instead of "text=StatusBarText.get()"?

Tofystedeth
+7  A: 

Since you're doing all of this in a single method call, the GUI never gets a chance to update before you start your sub process. Check out update_idletasks() call...

from http://infohost.nmt.edu/tcc/help/pubs/tkinter/universal.html

w.update_idletasks() Some tasks in updating the display, such as resizing and redrawing widgets, are called idle tasks because they are usually deferred until the application has finished handling events and has gone back to the main loop to wait for new events.
If you want to force the display to be updated before the application next idles, call the w.update_idletasks() method on any widget.

Trey Stout
Glad it worked :)
Trey Stout