wxpyton - 许多模块中的线程

时间:2014-03-13 23:17:12

标签: multithreading wxpython

我正在尝试创建将运行longrun过程的简单框架,但会在不同窗口(对话框)中显示计数,但它不起作用(staticText1未更新)...这是我的代码:

主框架(带一个按钮的简单框架)

import wx
import time
from threading import Thread
import Dialog1

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1BUTTON1, ] = [wx.NewId() for _init_ctrls in range(2)]

class Frame1(wx.Frame):
def test1(self):
    for i in range(self.delays):
        time.sleep(1)

def _init_ctrls(self, prnt):
    # generated method, don't edit
    wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
          pos=wx.Point(589, 259), size=wx.Size(178, 128),
          style=wx.DEFAULT_FRAME_STYLE, title='Frame1')

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

def __init__(self, parent):
    self._init_ctrls(parent)
    self.delays = 5
    self.timer = wx.Timer(self)

def OnButton1Button(self, event):
    self.testThread = Thread(target=self.test1)
    self.testThread.start()
    #self.Show(False)
    self.dlg = Dialog1.Dialog1(self)
    self.dlg.ShowModal()                    
    self.button1.Disable()
    self.Bind(wx.EVT_TIMER, self.PollThread)
    self.timer.Start(20, oneShot=True)

    event.Skip()
def PollThread(self, event):
    if self.testThread.isAlive():
        self.Bind(wx.EVT_TIMER, self.PollThread)
        self.timer.Start(200, oneShot=True)
        self.dlg.staticText1.SetLabel(self.dlg.staticText1.GetLabel()+".")
    else:
        self.button1.Enable()
        self.dlg.Destroy() 

Dialog(带有一个静态文本的简单对话框):

import wx

def create(parent):
    return Dialog1(parent)

[wxID_DIALOG1, wxID_DIALOG1STATICTEXT1, ] = [wx.NewId() for _init_ctrls in range(2)]

class Dialog1(wx.Dialog):
def _init_ctrls(self, prnt):
    # generated method, don't edit
    wx.Dialog.__init__(self, id=wxID_DIALOG1, name='', parent=prnt,
          pos=wx.Point(451, 295), size=wx.Size(544, 59),
          style=wx.DEFAULT_DIALOG_STYLE, title='Dialog1')

    self.staticText1 = wx.StaticText(id=wxID_DIALOG1STATICTEXT1,
          label=u'Please wait', name='staticText1', parent=self,
          pos=wx.Point(56, 24), size=wx.Size(95, 17), style=0)

def __init__(self, parent):
    self._init_ctrls(parent)

0 个答案:

没有答案