Name: [17088] Zimmer
Member: 62 months
Authored: 1 videos

Executing long process in wx Python without threads or other kludgy ways. [ID:1654] (1/1)

in series: Executing long process in wx Python without threads or other kludgy ways.

video tutorial by Zimmer, added 10/09

(Showmedo is undergoing major changes. To report any problems viewing the videos please email us and include browser and OS specifics. Cheers - Kyran.)

WxPython has a built in function to execute long functions easily, sadly it isn't well now... in this tutorial I show you how to use it. The function is wx.lib.delayedresult while a funny name is perfect ofr executing python tasks that are long and keeping your gui responsive...

#Boa:Frame:Frame1
import time
import wx
from wx.lib.delayedresult import startWorker as worker
def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1BUTTON2, wxID_FRAME1BUTTON3, 
 wxID_FRAME1PANEL1, wxID_FRAME1STATICTEXT1, 
] = [wx.NewId() for _init_ctrls in range(6)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(464, 373), size=wx.Size(400, 251),
              style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
        self.SetClientSize(wx.Size(392, 217))

        self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(392, 217),
              style=wx.TAB_TRAVERSAL)

        self.staticText1 = wx.StaticText(id=wxID_FRAME1STATICTEXT1,
              label='staticText1', name='staticText1', parent=self.panel1,
              pos=wx.Point(40, 40), size=wx.Size(312, 104), style=0)

        self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label='button1',
              name='button1', parent=self.panel1, pos=wx.Point(128, 176),
              size=wx.Size(75, 23), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
              id=wxID_FRAME1BUTTON1)

        self.button2 = wx.Button(id=wxID_FRAME1BUTTON2, label='button2',
              name='button2', parent=self.panel1, pos=wx.Point(48, 184),
              size=wx.Size(75, 23), style=0)
        self.button2.Bind(wx.EVT_BUTTON, self.OnButton2Button,
              id=wxID_FRAME1BUTTON2)

        self.button3 = wx.Button(id=wxID_FRAME1BUTTON3, label='button3',
              name='button3', parent=self.panel1, pos=wx.Point(208, 192),
              size=wx.Size(75, 23), style=0)
        self.button3.Bind(wx.EVT_BUTTON, self.OnButton3Button,
              id=wxID_FRAME1BUTTON3)

    def __init__(self, parent):
        self._init_ctrls(parent)
#The good stuff the OnButton.... is just what is called when you click a button

#As you can see they print the button clicked then after the worker is created

#What is the t.... stuff
    def OnButton1Button(self, event):
        print 'button1 clicked'
        worker(self.change_lable, t.call_just_to_call)
        print 'after worker'
    def OnButton2Button(self, event):
        print 'button2 clicked'
        worker(self.change_lable, t.change_to_true)
        print 'after worker'
    def OnButton3Button(self, event):
        print 'button3 clicked'
        worker(self.change_lable, t.change_to_false)
        print 'after worker'
    def change_lable(self, some_var):
        print some_var
        print t.var        

class test_class(object):
    def __init__(self):
        self.var = 'Unchanged'
    def change_to_true(self):
        time.sleep(10)
        self.var = True
    def change_to_false(self):
        time.sleep(10)
        self.var = False
    def call_just_to_call(self):
        time.sleep(10)

t = test_class()#An instance of test_class

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = create(None)
    frame.Show()

    app.MainLoop()

Got any questions?

Get answers in the ShowMeDo Learners Google Group.

Video statistics:

  • Video's rank shown in the most popular listing
  • Video plays: 606 (since July 30th)
  • Plays in last week: 0
  • Published: 59 months ago

Thank-yous, questions and comments

If this video tutorial was helpful please take some time to say thank-you to the authors for their hard work. Feel free to ask questions. Let the author know why their video tutorial was useful - what are you learning about? Did the video tutorial save you time? Would you like to see more?

You may also want to see our ShowMeDo Google Group to speak to our active users and authors.

Your email address will not be published.

Show some quick comments >>








All comments excluding tick-boxed quick-comments

4. anonymous Tue, 22 Dec 2009 02:09

Man, this is agony, PLEASE get a microphone!! :) That said, you actually do manage to get the gist of what you are achieving here across, and I found it useful!


Having no audio makes the screencast flow much less comfortable. But the contents are priceless. Thanks.


2. anonymous Tue, 13 Oct 2009 22:23

Thanks for the comment Kyran Dale, you the person who actually got me to get GUI programing (after trying ALOT). Also I am glad to hear that even with no audio the video was ok, :)


cool - and no need for subtitles ;) Always good to get more wxPython material. And, having used the technique demonstrated, it's a v. useful addition to anyone's wx-fu. Welcome to authorship and the club!


Showmedo is a peer-produced video-tutorials and screencasts site for free and open-source software (FOSS)- with the exception of some club videos, the large majority are free to watch and download.

how to help » about » faq »

Educating the Open-source Community With Showmedo

Although as important as the software it supports, education and documentation are relatively neglected in the Open-source world. Coders love to code, and explaining how best to use or improve the software tends to be deferred or even sidelined.

At Showmedo we believe the community can play a vital role here and also say thanks for the tools and software that make our lives easier. If you have a piece of software you love or a programming langugage you are enthusiastic about, why not make a screencast showing others how to use it? All the stuff you wish you'd been told, the tips, tricks, insights that would have saved you time and frustration.

Screencasting is easier than you think, and we're happy to help you. You can emailus for advice or just use some of the how-to screencasts on the site. This screencasting learning-pathis a good place to start.

By the Same Author

Content

Feedback

Showmedo's development is fairly rapid and bugs will inevitably creep in. If you have any problems please drop us a line using the contact address below. Likewise, any suggestions for improvements to the site are gratefully received.

feedback@showmedo.com