简体   繁体   中英

Threading wxPython GUI?

I've tried as many combination of threading as I know.

(wxPython "Frame1" created using wxFormBuilder)

This seems like it should work but it just crashes:

import wx
import wx.richtext
import time
import threading

class MyFrame1 ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        bSizer3 = wx.BoxSizer( wx.VERTICAL )

        self.m_staticText1 = wx.StaticText( self, wx.ID_ANY, u"This is a Thingy", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText1.Wrap( -1 )
        bSizer3.Add( self.m_staticText1, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )

        self.m_richText1 = wx.richtext.RichTextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0|wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER|wx.WANTS_CHARS )
        bSizer3.Add( self.m_richText1, 1, wx.EXPAND |wx.ALL, 5 )

        self.m_button2 = wx.Button( self, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer3.Add( self.m_button2, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )

        self.SetSizer( bSizer3 )
        self.Layout()

        self.Centre( wx.BOTH )

    def __del__( self ):
        pass

class Main(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        app = wx.App()
        frame = MyFrame1(None)
        frame.Show()
        app.MainLoop()

if __name__ == "__main__":

    print "Starting Thread!"
    t = Main()
    t.start()
    time.sleep(1)
    print "This should print while the window is open!"

Crash Dialog:

Python quit unexpectedly while using the libwx_osx_cocoau-3.0.0.1.0.dylib plug-in.

I've looked through numerous threads, read the "Non-Blocking Gui documentation" . It's not clicking for me.

OSX要求GUI保留在主线程中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM