博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
callAfter 例子1
阅读量:5251 次
发布时间:2019-06-14

本文共 3219 字,大约阅读时间需要 10 分钟。

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

你可能感兴趣的文章
关于收费软件
查看>>
getopt_long
查看>>
TensorFlow MNIST CNN 代码
查看>>
javascript之Style物
查看>>
JSON跨域解决方案收集
查看>>
图的深度优先遍历
查看>>
C# 之 提高WebService性能大数据量网络传输处理
查看>>
[bzoj1004] [HNOI2008] Cards
查看>>
原生HttpClient详细使用示例
查看>>
几道面试题
查看>>
Factory Design Pattern
查看>>
python中贪婪与非贪婪
查看>>
guava API整理
查看>>
无锁编程笔记
查看>>
jquery mobile
查看>>
如何在vue单页应用中使用百度地图
查看>>
Springboot使用步骤
查看>>
Spring属性注入
查看>>
Springboot-配置文件
查看>>
Springboot-日志框架
查看>>