user699540
user699540

Reputation: 849

Button Label change on EVT_BUTTON in wxpython?

Some system info before proceeding further:

Platform: Mac OS X 10.7.1

Python Version: ActiveState Python 2.7.1

wxPython Version: <a=http://downloads.sourceforge.net/wxpython/wxPython2.9-osx-2.9.2.1- cocoa-py2.7.dmg>wxPython2.9-osx-cocoa-py2.7

I want the button label to be changed while performing a task

So, here is what I did/want:

self.run_button=wx.Button(self.panel,ID_RUN_BUTTON,label='Install') self.Bind(wx.EVT_BUTTON, self.OnRun,id=ID_RUN_BUTTON)

def OnRun(self,evt): self.run_button.SetLabel('Installing..') #call a function that does the installation task installation_task() #After task completion, set the button label back to "Install" self.run_button.SetLabel('Install')

When I try doing this, it doesn't set the label to "Installing" while the task is being performed. Any suggestions how do I achieve this?

Upvotes: 3

Views: 6211

Answers (2)

Mike Driscoll
Mike Driscoll

Reputation: 33111

The "installation_task" function that you're running is blocking the GUI's mainloop. You need to update the label using threads, which means you need to learn wxPython's threadsafe methods. See the following articles: http://wiki.wxpython.org/LongRunningTasks or http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/

Upvotes: 2

FogleBird
FogleBird

Reputation: 76862

The button never gets a chance to redraw itself with the new label because you're running your logic (installation_task()) on the GUI thread.

You need to run installation_task() on a background thread so you don't lock up the GUI.

Upvotes: 3

Related Questions