wxPython update problem

Jason Smith jhs_bkk at nospam.yahoo.com
Mon Apr 7 12:22:38 EDT 2003


Nils Grimsmo wrote:

> run() is called when you push the button. run() sets the label for the
> button every second for four seconds, but the button label is not updated
> before you return from run().

Disclaimer:  I'm a week-old Python programmer from a long Perl background.

Maybe you should forego time.sleep() in an interactive GUI program? 
Sleeping is asking the kernel to knock your program completely out.  You
don't want to be sleeping when a user tries to click a button, after all! 
Although I'm new to GUI programming.

I had a similar problem (waiting for blocking I/O), and I used a wxPyTimer
to get control back to my program so at least the wxButton would pop back
up while the network talk ensued.  So that's what I used for your program. 
I have no idea if this is the right solution, however.  YMMV.

(Tested on Gentoo)

from wxPython.wx import *

class MainWindow(wxFrame):
    label  = ['foo', 'bar']
    button = None

    def __init__(self, parent, id, title):
        self.togcount = 0      # Counts the SetLabel calls

        wxFrame.__init__(self, parent, -4, title, size=(100,100))
        self.button = wxButton(self, 13, self.label[0])
        EVT_BUTTON(self, 13, self.run)
        self.Show(true)

    # Controls the toggle function
    def run(self, event):
        self.timer = wxPyTimer(self.toggleButton)
        self.timer.Start(1000)
        self.toggleButton()     # Gets the toggling going right now

    # Toggles the button
    def toggleButton(self):
        self.togcount += 1
        if self.togcount == 4:  # Done
            self.timer.Stop()
            self.togcount = 0

        self.button.SetLabel(self.label[self.togcount % 2])

app = wxPyApp()
frame = MainWindow(None, -1, 'Hello, World')
app.MainLoop()



----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---




More information about the Python-list mailing list