wxPython

Framework Code

  1. Simplest App:

    • MyApp(False)means noredirect while MyApp(True, 'output.txt') will redirect the output to the file output.txt.

    • Derivation from wx.App uses def OnInit(self) while others use def __init__(self)

    • Donnot forget return True

  2. Simplest Frame: frame.Show(), frame.Centre()

  3. A frame with menubar, toolbar, and statusbar: UpdateUIEvents are sent periodically by the framework during idle time to allow the application to check if the state of a control needs to be updated.

    • Append a menu_item with icon,

      qmi = wx.MenuItem(fileMenu, APP_EXIT, '&Quit\tCtrl+Q')
         qmi.SetBitmap(wx.Bitmap('exit.png'))
         fileMenu.AppendItem(qmi)
    • Menu check_item

      self.shtl = viewMenu.Append(wx.ID_ANY, 'Show toolbar', 'Show Toolbar', kind=wx.ITEM_CHECK)
         self.Bind(wx.EVT_MENU, self.ToggleStatusBar, self.shst)
         def ToggleStatusBar(self, e):        
             if self.shst.IsChecked():
              self.statusbar.Show()
             else:
                 self.statusbar.Hide()
  4. Pop a Dialogue box with validator: derivation from wx.PyValidator

  5. A login dialogue before main frame: wx.PyValidator

  6. Notebook with multiple pages

  7. Advanced notebook, user can add new pages

  8. Foldable Pannel

  9. Splash Screen: splash before entering main frame wx.SplashScreen

  10. Extendable Pannel

  11. SplitPanel (hide, show): a horizontally or vertically splited panel, loading html file

  12. File Drag Drop

  13. File Hunter

  14. SpreadSheet

  15. Media Player

  16. Web Browser


Component Code

  1. Bitmap: use bitmap to beautify the appearance, or use the bitmap brush (transparent widgets may be a little troublesome here)

    self.Bind(wx.EVT_PAINT, self.OnPaint)
     def OnPaint(self, event):
         dc = wx.PaintDC(self)
         dc.SetBackgroundMode(wx.TRANSPARENT)
         brush1 = wx.BrushFromBitmap(wx.Bitmap('pattern1.jpg'))
         dc.SetBrush(brush1)
         w, h =  self.GetSize()
         dc.DrawRectangle(0, 0, w, h)
  2. Simplest Button: use lib to build advanced buttons

    • Bind function with building blocks
      self.Bind(wx.EVT_BUTTON, self.OnButton, button) 
         def OnButton(self, event):
    • Note GetChildren(), GetParent(), GetId(), FindWindowById(self.btnId)
  3. Advanced button: bitmap button, toggle button, gradient button

  4. Frame Icon: personalize the frame icon

    con = wx.Icon(path, wx.BITMAP_TYPE_PNG)
     self.SetIcon(icon):
  5. Interaction with Clipboard: paste to and copy from system clipboard

  6. Drop Files to Frame: : wx.PyDropTarget

  7. Help in Frame: when initializing Frame

    pre = wx.PreFrame()
     pre.SetExtraStyle(wx.FRAME_EX_CONTEXTHELP)
     pre.Create(parent, *args, **kwargs)
     self.PostCreate(pre)
  8. Checkbox: use more general event detector to simplify code

    `self.Bind(wx.EVT_CHECKBOX, self.OnCheck)`
    

    e_obj = event.GetEventObject()

  9. dropdown menu

  10. MessageBox

    def ShowMessage(self,event):
         wx.MessageBox('Download completed', 'Info', wx.OK | wx.ICON_INFORMATION)
  11. Open file_dialogue

    dlg = wx.FileDialog(self, "Open File", style=wx.FD_OPEN)
     if dlg.ShowModal() == wx.ID_OK:
         fname = dlg.GetPath()
         handle = open(fname, 'r')
         self.txtctrl.SetValue(handle.read())
         handle.close()
  12. popup menu: right click to pop up the menu

  13. static box: a static box containing components

  14. list ctrl: multi-column list

  15. customtree: a tree-structure file browser

  16. virtual list box

  17. Styled Text: i.e., python-style text

  18. Download Progressbar

  19. About info: info including name, version, copyright, and description

  20. Choose Color Dialogue

    colour_data = wx.ColourData()
     colour = self.GetBackgroundColour()
     colour_data.SetColour(colour)
     colour_data.SetChooseFull(True)
    
     dlg = wx.ColourDialog(self, colour_data)
     if dlg.ShowModal() == wx.ID_OK:
         colour = dlg.GetColourData().GetColour()
         self.SetBackgroundColour(colour)
         self.Refresh()
     dlg.Destroy()
  21. Image Browser with simple editing

  22. Image Slide Show

  23. Search Bar

  24. Timer

    self._timer = wx.Timer(self)
     self.Bind(wx.EVT_TIMER, self.OnTimer, self._timer)
     self._timer.Start(100)
     self._timer.Stop()
  25. Execute Command Line

    import outputwin
     self.output = outputwin.OutputWindow(self)
     self.output.StartProcess("ping %s" % url, blocksize=64)
  26. Music Player

  27. Video Player: First, you need to install MplayerCtrl lib. Secondly, place the mplayer folder under the current working directory.


Layout

  1. wx.BoxSizer: proportion is used to control main direction and wx.EXPAND is used to control the other direction. Note in BoxSizer, alignment is only valid in one direction. AddSpacer(50) is equal to Add((50,50)). AddStretchSpacer() is equal to Add((0,0),proportion=1).

    sizer = wx.BoxSizer(wx.HORIZONTAL)
     sizer.AddSpacer(50)
     sizer.Add(sth,proportion=0, flag=wx.ALL, border=5) #use flag to mark which side has border
     sizer.Add((-1,10)) #add a black space, height=10
     # sizer.Add(sth,proportion=0, wx.EXPAND|wx.RIGHT|wx.ALIGN_RIGHT, border=5)
     sizer.AddSpacer((0,0)) #sizer.AddStretchSpacer()
     self.SetSizer(sizer)
     self.SetInitialSize()
  2. wx.GridSizer: proportion is usually set as 0, use Add((20,20), 1, wx.EXPAND) to take up space.

    wx.GridSizer(2, 2, vgap=0, hgap=0)
     msizer.Add(sth, 0, wx.EXPAND)
  3. wx.FlexGridSizer: make some rows and columns growable.

    fgs.AddGrowableRow(2)
     fgs.AddGrowableCol(1)
  4. wx.GridBagSizer: use pos and span to indicate the location and size.

    sizer = wx.GridBagSizer(vgap=8, hgap=8)
     sizer.Add(sth, (1, 2), (1, 15), wx.EXPAND) 

Notes

  1. Event Propagation: When an event can intrigue multiple events, use event.skip() to guaranttee the occurrence of following events. Take keyevents.py for an example.

  2. Virtual Ride: wx.PyPannel

  3. Bind function which will be checked in the idle time
    self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateEditMenu)