PYTHON   39

wxdemo.py

Guest on 3rd September 2021 08:23:02 PM

  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3.  
  4. """Demo of wxPython integration with Twisted."""
  5.  
  6. import sys
  7.  
  8. from wx import Frame, DefaultPosition, Size, Menu, MenuBar, App
  9. from wx import EVT_MENU, EVT_CLOSE
  10.  
  11. from twisted.python import log
  12. from twisted.internet import wxreactor
  13. wxreactor.install()
  14.  
  15. # import t.i.reactor only after installing wxreactor:
  16. from twisted.internet import reactor
  17.  
  18.  
  19. ID_EXIT  = 101
  20.  
  21. class MyFrame(Frame):
  22.     def __init__(self, parent, ID, title):
  23.         Frame.__init__(self, parent, ID, title, DefaultPosition, Size(300, 200))
  24.         menu = Menu()
  25.         menu.Append(ID_EXIT, "E&xit", "Terminate the program")
  26.         menuBar = MenuBar()
  27.         menuBar.Append(menu, "&File")
  28.         self.SetMenuBar(menuBar)
  29.         EVT_MENU(self, ID_EXIT,  self.DoExit)
  30.        
  31.         # make sure reactor.stop() is used to stop event loop:
  32.         EVT_CLOSE(self, lambda evt: reactor.stop())
  33.  
  34.     def DoExit(self, event):
  35.         reactor.stop()
  36.  
  37.  
  38. class MyApp(App):
  39.  
  40.     def twoSecondsPassed(self):
  41.         print "two seconds passed"
  42.  
  43.     def OnInit(self):
  44.         frame = MyFrame(None, -1, "Hello, world")
  45.         frame.Show(True)
  46.         self.SetTopWindow(frame)
  47.         # look, we can use twisted calls!
  48.         reactor.callLater(2, self.twoSecondsPassed)
  49.         return True
  50.  
  51.  
  52. def demo():
  53.     log.startLogging(sys.stdout)
  54.  
  55.     # register the App instance with Twisted:
  56.     app = MyApp(0)
  57.     reactor.registerWxApp(app)
  58.  
  59.     # start the event loop:
  60.     reactor.run()
  61.  
  62.  
  63. if __name__ == '__main__':
  64.     demo()

Raw Paste


Login or Register to edit or fork this paste. It's free.