zoukankan      html  css  js  c++  java
  • CallAfter

    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.

     

    切换行号显示
    import threading,wx
    
    ID_RUN=101
    ID_RUN2=102
    
    class MyFrame(wx.Frame):
        def __init__(self, parent, ID, title):
            wx.Frame.__init__(self, parent, ID, title)
            panel = wx.Panel(self, -1)
            mainSizer=wx.BoxSizer(wx.HORIZONTAL)
            mainSizer.Add(wx.Button(panel, ID_RUN, "Click me"))
            mainSizer.Add(wx.Button(panel, ID_RUN2, "Click me too"))
            panel.SetSizer(mainSizer)
            mainSizer.Fit(self)
            wx.EVT_BUTTON(self, ID_RUN, self.onRun)
            wx.EVT_BUTTON(self, ID_RUN2, self.onRun2)
    
        def onRun(self,event):
            print "Clicky!"
            wx.CallAfter(self.AfterRun, "I don't appear until after OnRun exits")
            s=raw_input("Enter something:")
            print s
    
        def onRun2(self,event):
            t=threading.Thread(target=self.__run)
            t.start()
    
        def __run(self):
            wx.CallAfter(self.AfterRun, "I appear immediately (event handler
    "+ 
                            "exited when OnRun2 finished)")
            s=raw_input("Enter something in this thread:")
            print s
    
        def AfterRun(self,msg):
            dlg=wx.MessageDialog(self, msg, "Called after", wx.OK|wx.ICON_INFORMATION)
            dlg.ShowModal()
            dlg.Destroy()
    
    
    class MyApp(wx.App):
        def OnInit(self):
            frame = MyFrame(None, -1, "CallAfter demo")
            frame.Show(True)
            frame.Centre()
            return True
    
    app = MyApp(0)
    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
    "+ 
                            "exited when OnRun2 finished)")

    to

    self.AfterRun("I appear immediately (event handler
    "+ 
                            "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.

  • 相关阅读:
    软件工程14—第09组 Beta冲刺(2/4)
    软件工程13—第09组 Beta冲刺(1/4)
    软件工程12—第09组 Alpha事后诸葛
    软件工程11—第09组 Alpha冲刺(4/4)
    软件工程10—第09组 Alpha冲刺(3/4)
    软件工程09—第09组 Alpha冲刺(2/4)
    软件工程08—第09组 Alpha冲刺(1/4)
    软件工程07—第09组 团队Git现场编程实战
    软件工程06—亚瑟王の十三水2.0
    第06组 Alpha冲刺(4/6)
  • 原文地址:https://www.cnblogs.com/dengyigod/p/3304592.html
Copyright © 2011-2022 走看看