8 Replies - 1043 Views - Last Post: 28 August 2011 - 10:08 PM Rate Topic: -----

#1 imperialguy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 28-August 11

Button Label change on EVT_BUTTON in wxpython!

Posted 28 August 2011 - 06:27 PM

Some system info before proceeding further:

Platform: Mac OS X 10.7.1
Python Version: ActiveState Python 2.7.1
wxPython Version: 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?

Is This A Good Question/Topic? 0
  • +

Replies To: Button Label change on EVT_BUTTON in wxpython!

#2 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Button Label change on EVT_BUTTON in wxpython!

Posted 28 August 2011 - 08:48 PM

Could you post a complete, runnable example of what's not working for you?

This link shows what you're talking about, and it works for me.

What does installation_task do? It may be that the second SetLabel (to Install) is happening so quickly that you don't see the change.
Was This Post Helpful? 0
  • +
  • -

#3 imperialguy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 28-August 11

Re: Button Label change on EVT_BUTTON in wxpython!

Posted 28 August 2011 - 09:13 PM

View PostHiram, on 28 August 2011 - 08:48 PM, said:

Could you post a complete, runnable example of what's not working for you?

This link shows what you're talking about, and it works for me.

What does installation_task do? It may be that the second SetLabel (to Install) is happening so quickly that you don't see the change.


Thanks for the reply. Actually, it takes at least 1 minute for the installation task to run. So, there is plenty of time during which I want the button to show the "Installing" label. The installation task just downloads a bunch of files from a repository residing on another server (so, no GUI-related operations are happening during that time). I did go through that tutorial before posting this thread. It wasn't of much help.
Was This Post Helpful? 0
  • +
  • -

#4 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Button Label change on EVT_BUTTON in wxpython!

Posted 28 August 2011 - 09:35 PM

Hmm, well that's strange. Well, could you post a runnable example of this not working for you?
Was This Post Helpful? 0
  • +
  • -

#5 imperialguy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 28-August 11

Re: Button Label change on EVT_BUTTON in wxpython!

Posted 28 August 2011 - 09:42 PM

View Postimperialguy, on 28 August 2011 - 09:13 PM, said:

View PostHiram, on 28 August 2011 - 08:48 PM, said:

Could you post a complete, runnable example of what's not working for you?

This link shows what you're talking about, and it works for me.

What does installation_task do? It may be that the second SetLabel (to Install) is happening so quickly that you don't see the change.


Thanks for the reply. Actually, it takes at least 1 minute for the installation task to run. So, there is plenty of time during which I want the button to show the "Installing" label. The installation task just downloads a bunch of files from a repository residing on another server (so, no GUI-related operations are happening during that time). I did go through that tutorial before posting this thread. It wasn't of much help.


To make it more clear, forget about the "installation task". Let's say I use a sleep timer, even then it doesn't work. Below:

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..')
	time.sleep(3)
	self.run_button.SetLabel('Install')


According to the above code, the button press should set the label to "Installing". And after 3 sec it should turn back to "Install". This doesn't happen though.

This post has been edited by imperialguy: 28 August 2011 - 09:42 PM

Was This Post Helpful? 0
  • +
  • -

#6 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Button Label change on EVT_BUTTON in wxpython!

Posted 28 August 2011 - 09:48 PM

Yes, well, I can't test and debug that. That method is most definitely inside a class. If I could run your code, I could debug it. That's why I'm asking for a runnable example you see :P
Was This Post Helpful? 0
  • +
  • -

#7 imperialguy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 28-August 11

Re: Button Label change on EVT_BUTTON in wxpython!

Posted 28 August 2011 - 09:56 PM

View PostHiram, on 28 August 2011 - 09:48 PM, said:

Yes, well, I can't test and debug that. That method is most definitely inside a class. If I could run your code, I could debug it. That's why I'm asking for a runnable example you see :P


This code runs on my Mac (same concept, but different labels). You might have to change the shebang line according to your Mac's settings.

#!/usr/bin/env /usr/local/bin/python
import wx,time

class ButtonFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Button Example', size=(300, 100))
        panel = wx.Panel(self, -1)
        self.button = wx.Button(panel, -1, "Button", pos=(50, 20))
        self.Bind(wx.EVT_BUTTON, self.onclick, self.button)
        self.button.SetDefault()

    def onclick(self, event):
        self.button.SetLabel("Clicked")
        time.sleep(3)
        self.button.SetLabel("Button")
        
app = wx.PySimpleApp()
frame = ButtonFrame()
frame.Show()
app.MainLoop()


Was This Post Helpful? 0
  • +
  • -

#8 imperialguy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 28-August 11

Re: Button Label change on EVT_BUTTON in wxpython!

Posted 28 August 2011 - 10:02 PM

Got the solution. Using wx.Yield() solves the problem. Here's the link.
Was This Post Helpful? 1
  • +
  • -

#9 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Button Label change on EVT_BUTTON in wxpython!

Posted 28 August 2011 - 10:08 PM

Great! That's much better than what I was hacking together, haha. My solution had a separate thread for the processing, which would update the button's label upon completion.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1