Адаптировано с http://wiki.wxpython.org/LongRunningTasks
import wx
import thread
class MainFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
self.btn = wx.Button(self, label="Start")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.btn, proportion=0, flag=wx.EXPAND)
self.SetSizerAndFit(sizer)
self.Bind(wx.EVT_BUTTON, self.onButton)
def onButton(self, evt):
self.btn.Enable(False)
thread.start_new_thread(self.longRunning, ())
def onLongRunDone(self):
print "finished my task, I may want to update GUI elements here"
self.btn.Enable(True)
def longRunning(self):
f = open("hello.txt",'wb')
datatowrite = urllib.urlopen(link).read()
f.write(datatowrite)
f.close()
wx.CallAfter(self.onLongRunDone)
if __name__ == "__main__":
app = wx.PySimpleApp()
app.TopWindow = MainFrame(None)
app.TopWindow.Show()
app.MainLoop()