wxPython before MainLoop

7stud bbxx789_05ss at yahoo.com
Thu Aug 9 09:50:07 CEST 2007


On Aug 8, 11:25 pm, "[david]" <da... at nospam.spam> wrote:
> I'd like to refresh the display before I start the main loop.
>
> I have code like this:
>
> app = App()
> app.Show()
> app.long_slow_init()
> app.MainLoop()
>
> The main frame partly loads at Show, but because the mainloop has not
> started yet, the display does not update until long_slow_init() finishes.
>
> Alternatively, I could code
>
> app = App()
> app.long_slow_init()
> app.Show()
> app.MainLoop()
>
> Which would give me a crisp Show, but there would be a long slow wait
> before the app showed any activity at all. I would need a splash screen.
>
> I'd rather not have a splash screen (and I don't know how anyway). I'd
> like to just make app.Show() finish correctly before running
> long_slow_init.
>
> Is there a wx internal method that I can use to give Windows the
> opportunity to finish painting the frame before I run long_slow_init()?
>
> Or is there a better idea?
>
> (david)

You can use a separate thread to execute long_slow_init():

--------------------------
import wx
import threading
import time

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "My Window")

        panel = wx.Panel(self, -1)
        button = wx.Button(panel, -1, "click me, quick!", pos=(40,
40))
        self.Bind(wx.EVT_BUTTON, self.onclick)

    def onclick(self, event):
        print "button clicked"

    def receive_result(self, result):
        print "Hey, I'm done with that long, slow initialization."
        print "The result was:", result


class MyApp(wx.App):
    def __init__(self):
        wx.App.__init__(self, redirect=False)


    def OnInit(self):
        the_frame = MyFrame()
        the_frame.Show()

        t = MyThread(the_frame)
        t.start()   #calls run() in t's class

        return True


class MyThread(threading.Thread):
    def __init__(self, a_frame):
        threading.Thread.__init__(self)

        self.frame_obj = a_frame

    def run(self):
        self.result = self.long_slow_init()

    def long_slow_init(self):
        print "starting long_slow_init()..."
        time.sleep(6)
        result = 20.5

        #Send result to frame:
        wx.CallAfter(self.frame_obj.receive_result, result)

app = MyApp()
app.MainLoop()
----------------------------





More information about the Python-list mailing list