callAfter 例子1

wx.CallAfter takes a function and its arguments, and calls the function when the current event handler has exited. It is a safe way of requesting action of a Window from another thread. The code below has a couple of examples.

 

切换行号显示
   1 import threading,wx
   2 
   3 ID_RUN=101
   4 ID_RUN2=102
   5 
   6 class MyFrame(wx.Frame):
   7     def __init__(self, parent, ID, title):
   8         wx.Frame.__init__(self, parent, ID, title)
   9         panel = wx.Panel(self, -1)
  10         mainSizer=wx.BoxSizer(wx.HORIZONTAL)
  11         mainSizer.Add(wx.Button(panel, ID_RUN, "Click me"))
  12         mainSizer.Add(wx.Button(panel, ID_RUN2, "Click me too"))
  13         panel.SetSizer(mainSizer)
  14         mainSizer.Fit(self)
  15         wx.EVT_BUTTON(self, ID_RUN, self.onRun)
  16         wx.EVT_BUTTON(self, ID_RUN2, self.onRun2)
  17 
  18     def onRun(self,event):
  19         print "Clicky!"
  20         wx.CallAfter(self.AfterRun, "I don't appear until after OnRun exits")
  21         s=raw_input("Enter something:")
  22         print s
  23 
  24     def onRun2(self,event):
  25         t=threading.Thread(target=self.__run)
  26         t.start()
  27 
  28     def __run(self):
  29         wx.CallAfter(self.AfterRun, "I appear immediately (event handler\n"+ \
                        "exited when OnRun2 finished)")
  30         s=raw_input("Enter something in this thread:")
  31         print s
  32 
  33     def AfterRun(self,msg):
  34         dlg=wx.MessageDialog(self, msg, "Called after", wx.OK|wx.ICON_INFORMATION)
  35         dlg.ShowModal()
  36         dlg.Destroy()
  37 
  38 
  39 class MyApp(wx.App):
  40     def OnInit(self):
  41         frame = MyFrame(None, -1, "CallAfter demo")
  42         frame.Show(True)
  43         frame.Centre()
  44         return True
  45 
  46 app = MyApp(0)
  47 app.MainLoop()

When "Click me" is pressed, onRun is called. It immediately prints "Clicky!" and issues three calls to wx.CallAfter, then waits for you to enter some text via stdin, prints the text, and returns. At this point, all pending events have been dealt with, and the commands given to CallAfter are executed. Note that the dialogs appear in reverse order.

When "Click me too" is pressed, onRun2 is called. It spawns a thread and returns. The thread executes __run, which calls CallAfter, waits for you to enter some text, prints it, and returns. In this case, however, the dialog now appears before you enter the text. This is because the event handler has already exited; the dialog was created from a thread. You can type at the prompt or close the dialog, whichever you prefer. You can even close the dialog and click on a button again before typing.

If CallAfter is not used in this second circumstance, crashes occur. Change

wx.CallAfter(self.AfterRun, "I appear immediately (event handler\n"+ \
                        "exited when OnRun2 finished)")

to

self.AfterRun("I appear immediately (event handler\n"+ \
                        "exited when OnRun2 finished)")

Run the program and click on "Click me too". Dismiss the dialog box and click either button again (before typing anything) to witness the event dispatch thread becoming very confused.

转载于:https://www.cnblogs.com/dengyigod/archive/2013/05/22/3094005.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值