Framework Code
-
MyApp(False)
means noredirect whileMyApp(True, 'output.txt')
will redirect the output to the file output.txt.Derivation from wx.App uses
def OnInit(self)
while others usedef __init__(self)
Donnot forget
return True
Simplest Frame:
frame.Show()
,frame.Centre()
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()
Pop a Dialogue box with validator: derivation from
wx.PyValidator
A login dialogue before main frame:
wx.PyValidator
-
Splash Screen: splash before entering main frame
wx.SplashScreen
-
SplitPanel (hide, show): a horizontally or vertically splited panel, loading html file
-
-
-
-
-
Component Code
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)
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)
- Bind function with building blocks
Advanced button: bitmap button, toggle button, gradient button
Frame Icon: personalize the frame icon
con = wx.Icon(path, wx.BITMAP_TYPE_PNG) self.SetIcon(icon):
Interaction with Clipboard: paste to and copy from system clipboard
Drop Files to Frame: :
wx.PyDropTarget
Help in Frame: when initializing Frame
pre = wx.PreFrame() pre.SetExtraStyle(wx.FRAME_EX_CONTEXTHELP) pre.Create(parent, *args, **kwargs) self.PostCreate(pre)
Checkbox: use more general event detector to simplify code
`self.Bind(wx.EVT_CHECKBOX, self.OnCheck)`
e_obj = event.GetEventObject()
MessageBox
def ShowMessage(self,event): wx.MessageBox('Download completed', 'Info', wx.OK | wx.ICON_INFORMATION)
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()
popup menu: right click to pop up the menu
static box: a static box containing components
list ctrl: multi-column list
customtree: a tree-structure file browser
-
Styled Text: i.e., python-style text
About info: info including name, version, copyright, and description
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()
Timer
self._timer = wx.Timer(self) self.Bind(wx.EVT_TIMER, self.OnTimer, self._timer) self._timer.Start(100) self._timer.Stop()
Execute Command Line
import outputwin self.output = outputwin.OutputWindow(self) self.output.StartProcess("ping %s" % url, blocksize=64)
Video Player: First, you need to install MplayerCtrl lib. Secondly, place the mplayer folder under the current working directory.
Layout
wx.BoxSizer
:proportion
is used to control main direction andwx.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()
wx.GridSizer
:proportion
is usually set as 0, useAdd((20,20), 1, wx.EXPAND)
to take up space.wx.GridSizer(2, 2, vgap=0, hgap=0) msizer.Add(sth, 0, wx.EXPAND)
wx.FlexGridSizer
: make some rows and columns growable.fgs.AddGrowableRow(2) fgs.AddGrowableCol(1)
wx.GridBagSizer
: usepos
andspan
to indicate the location and size.sizer = wx.GridBagSizer(vgap=8, hgap=8) sizer.Add(sth, (1, 2), (1, 15), wx.EXPAND)
Notes
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.Virtual Ride:
wx.PyPannel
Bind function which will be checked in the idle time
self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateEditMenu)