Attachment 'CustomCheckBoxDemo.py'

Download

   1 import wx
   2 import wx.lib.colourselect as csel
   3 
   4 import CustomCheckBox as CCB
   5 
   6 
   7 #----------------------------------------------------------------------
   8 def GetMondrianData():
   9     return \
  10 '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00 \x00\x00\x00 \x08\x06\x00\
  11 \x00\x00szz\xf4\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\x00\x00qID\
  12 ATX\x85\xed\xd6;\n\x800\x10E\xd1{\xc5\x8d\xb9r\x97\x16\x0b\xad$\x8a\x82:\x16\
  13 o\xda\x84pB2\x1f\x81Fa\x8c\x9c\x08\x04Z{\xcf\xa72\xbcv\xfa\xc5\x08 \x80r\x80\
  14 \xfc\xa2\x0e\x1c\xe4\xba\xfaX\x1d\xd0\xde]S\x07\x02\xd8>\xe1wa-`\x9fQ\xe9\
  15 \x86\x01\x04\x10\x00\\(Dk\x1b-\x04\xdc\x1d\x07\x14\x98;\x0bS\x7f\x7f\xf9\x13\
  16 \x04\x10@\xf9X\xbe\x00\xc9 \x14K\xc1<={\x00\x00\x00\x00IEND\xaeB`\x82' 
  17 
  18 def GetMondrianBitmap():
  19     return wx.BitmapFromImage(GetMondrianImage())
  20 
  21 def GetMondrianImage():
  22     import cStringIO
  23     stream = cStringIO.StringIO(GetMondrianData())
  24     return wx.ImageFromStream(stream)
  25 
  26 def GetMondrianIcon():
  27     icon = wx.EmptyIcon()
  28     icon.CopyFromBitmap(GetMondrianBitmap())
  29     return icon
  30 
  31 
  32 #---------------------------------------------------------------------------
  33 # Show how to derive a custom wxLog class
  34 
  35 class MyLog(wx.PyLog):
  36 
  37     def __init__(self, textCtrl, logTime=0):
  38 
  39         wx.PyLog.__init__(self)
  40         self.tc = textCtrl
  41         self.logTime = logTime
  42 
  43 
  44     def DoLogString(self, message, timeStamp):
  45 
  46         if self.tc:
  47             self.tc.AppendText(message + '\n')
  48 
  49 #---------------------------------------------------------------------------
  50 
  51 class CustomCheckBoxDemo(wx.Frame):
  52 
  53     def __init__(self, parent, id=wx.ID_ANY, title="", pos=wx.DefaultPosition,
  54                  size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE):
  55 
  56         wx.Frame.__init__(self, parent, id, title, pos, size, style)
  57         self.SetIcon(GetMondrianIcon())
  58 
  59         self.mainPanel = wx.Panel(self, -1)
  60 
  61         # Set up a log window
  62         self.logWindow = wx.TextCtrl(self.mainPanel, -1, size=(-1, 100),
  63                                      style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
  64         # Set the wxWidgets log target to be this textctrl
  65         wx.Log_SetActiveTarget(MyLog(self.logWindow))
  66 
  67         # Create some CustomCheckBoxes
  68         self.ccb1 = CCB.CustomCheckBox(self.mainPanel, -1, "Hello wxPython!")
  69         self.ccb2 = CCB.CustomCheckBox(self.mainPanel, -1, "CustomCheckBox 1")
  70         self.ccb3 = CCB.CustomCheckBox(self.mainPanel, -1, "CustomCheckBox 2")
  71         self.ccb4 = CCB.CustomCheckBox(self.mainPanel, -1, "CustomCheckBox 3")
  72 
  73         # Create a button that enables/disables the first CustomCheckBox
  74         self.buttonEnabler = wx.Button(self.mainPanel, -1, "Disable")
  75 
  76         # Create a button that "toggles" the CustomCheckBox state
  77         self.buttonToggler = wx.Button(self.mainPanel, -1, "Toggle")
  78 
  79         # Create a button that changes the font of self.ccb2
  80         self.buttonFont = wx.Button(self.mainPanel, -1, "Set Font")
  81 
  82         # Create a button that changes the background colour of self.ccb3
  83         backColour = self.ccb3.GetBackgroundColour()
  84         self.buttonBack = csel.ColourSelect(self.mainPanel, -1, "Background",
  85                                             backColour)
  86 
  87         # Create a button that changes the foreground colour of self.ccb4
  88         foreColour = self.ccb4.GetForegroundColour()
  89         self.buttonFore = csel.ColourSelect(self.mainPanel, -1, "Foreground",
  90                                             foreColour)
  91         
  92         # Layout the items with sizers
  93         self.LayoutItems()
  94 
  95         # Bind the events
  96         self.BindEvents()
  97 
  98 
  99     def LayoutItems(self):
 100 
 101         mainSizer = wx.BoxSizer(wx.VERTICAL)
 102         panelSizer = wx.BoxSizer(wx.VERTICAL)
 103         centerSizer = wx.BoxSizer(wx.HORIZONTAL)
 104         leftSizer = wx.BoxSizer(wx.HORIZONTAL)
 105         buttonSizer = wx.BoxSizer(wx.VERTICAL)
 106         rightSizer = wx.BoxSizer(wx.VERTICAL)
 107         checkSizer1 = wx.BoxSizer(wx.HORIZONTAL)
 108         checkSizer2 = wx.BoxSizer(wx.HORIZONTAL)
 109         checkSizer3 = wx.BoxSizer(wx.HORIZONTAL)
 110 
 111         # Add the buttons to the right sizer
 112         buttonSizer.Add(self.buttonEnabler, 0, wx.ALL, 5)
 113         buttonSizer.Add(self.buttonToggler, 0, wx.ALL, 5)
 114         leftSizer.Add(self.ccb1, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 10)
 115         leftSizer.Add(buttonSizer, 0, wx.ALIGN_CENTER_VERTICAL)
 116 
 117         # Add the other 3 CustomCheckBoxes and their buttons
 118         checkSizer1.Add(self.ccb2, 0, wx.RIGHT|wx.ALIGN_CENTER_VERTICAL, 10)
 119         checkSizer1.Add(self.buttonFont, 0)
 120         checkSizer2.Add(self.ccb3, 0, wx.RIGHT|wx.ALIGN_CENTER_VERTICAL, 10)
 121         checkSizer2.Add(self.buttonBack, 0)
 122         checkSizer3.Add(self.ccb4, 0, wx.RIGHT|wx.ALIGN_CENTER_VERTICAL, 10)
 123         checkSizer3.Add(self.buttonFore, 0)
 124         
 125         rightSizer.Add(checkSizer1, 0, wx.BOTTOM, 5)
 126         rightSizer.Add(checkSizer2, 0, wx.BOTTOM, 5)
 127         rightSizer.Add(checkSizer3, 0)
 128 
 129         centerSizer.Add(leftSizer, 0, wx.ALL, 30)
 130         centerSizer.Add(rightSizer, 0, wx.TOP|wx.LEFT, 25)
 131 
 132         panelSizer.Add(centerSizer, 1, wx.EXPAND)
 133         panelSizer.Add(self.logWindow, 0, wx.EXPAND)
 134 
 135         # Set the sizer to the panel        
 136         self.mainPanel.SetSizer(panelSizer)
 137 
 138         # Add the panel to the frame sizer        
 139         mainSizer.Add(self.mainPanel, 1, wx.EXPAND)
 140         
 141         self.SetSizer(mainSizer)
 142         mainSizer.Layout()
 143 
 144 
 145     def BindEvents(self):
 146 
 147         # Ok, let's bind some events...
 148         
 149         # All the CustomCheckBoxes go to the same event handler, as we don't
 150         # need a separate handler for every CustomCheckBox
 151         self.Bind(wx.EVT_CHECKBOX, self.OnCheckBox, self.ccb1)
 152         self.Bind(wx.EVT_CHECKBOX, self.OnCheckBox, self.ccb2)
 153         self.Bind(wx.EVT_CHECKBOX, self.OnCheckBox, self.ccb3)
 154         self.Bind(wx.EVT_CHECKBOX, self.OnCheckBox, self.ccb4)
 155 
 156         # Now we take care of the buttons
 157         self.Bind(wx.EVT_BUTTON, self.OnEnable, self.buttonEnabler)
 158         self.Bind(wx.EVT_BUTTON, self.OnToggle, self.buttonToggler)
 159         self.Bind(wx.EVT_BUTTON, self.OnFont, self.buttonFont)
 160 
 161         self.buttonBack.Bind(csel.EVT_COLOURSELECT, self.OnBackground)
 162         self.buttonFore.Bind(csel.EVT_COLOURSELECT, self.OnForeground)
 163 
 164 
 165     def OnCheckBox(self, event):
 166 
 167         # Grab the CustomCheckBox that generated the event
 168         control = event.GetEventObject()
 169 
 170         # Get its label
 171         label = control.GetLabel()
 172 
 173         # Get the checked/unchecked value
 174         value = event.IsChecked()
 175 
 176         # Display the label and some info in the wx.LogTextCtrl
 177         self.logWindow.AppendText("CustomCheckBox event from ==> " + label + \
 178                                   ", checked = " + repr(value) + "\n")
 179 
 180 
 181     def OnEnable(self, event):
 182 
 183         if self.ccb1.IsEnabled():
 184             # we are enabled, so let's disable ourselves and also change the
 185             # button label to "Enable"
 186             self.ccb1.Enable(False)
 187             self.buttonEnabler.SetLabel("Enable")
 188         else:
 189             # we are disabled, so let's enable ourselves and also change the
 190             # button label to "Disable"
 191             self.ccb1.Enable(True)
 192             self.buttonEnabler.SetLabel("Disable")
 193             
 194 
 195     def OnToggle(self, event):
 196 
 197         if self.ccb1.IsChecked():
 198             # we are checked, so let's go for unchecking...
 199             self.ccb1.SetValue(0)
 200         else:
 201             # we are unchecked, so let's go for checking...
 202             self.ccb1.SetValue(1)
 203             
 204 
 205     def OnFont(self, event):
 206 
 207         initialFont = self.ccb2.GetFont()
 208         if not initialFont:
 209             font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
 210             
 211         data = wx.FontData()
 212         data.SetInitialFont(initialFont)
 213         dlg = wx.FontDialog(self, data)
 214         
 215         if dlg.ShowModal() == wx.ID_OK:
 216             data = dlg.GetFontData()
 217             font = data.GetChosenFont()
 218             self.ccb2.SetFont(font)
 219             self.ccb2.GetContainingSizer().Layout()
 220             
 221         # Don't destroy the dialog until you get everything you need from the
 222         # dialog!
 223         dlg.Destroy()
 224 
 225         
 226     def OnBackground(self, event):
 227 
 228         # We change the background colour
 229         colour = event.GetValue()        
 230         self.ccb3.SetBackgroundColour(colour) 
 231         
 232 
 233     def OnForeground(self, event):
 234 
 235         # We change the foreground colour
 236         colour = event.GetValue()        
 237         self.ccb4.SetForegroundColour(colour) 
 238 
 239 
 240 def main():
 241 
 242     app = wx.PySimpleApp()
 243     frame = CustomCheckBoxDemo(None, -1, "CustomCheckBox wxPython Demo ;-)",
 244                                size=(600, 400))
 245     frame.CenterOnScreen()
 246     frame.Show()
 247     app.MainLoop()
 248 
 249 
 250 if __name__ == "__main__":
 251     main()
 252 
 253     

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-11-30 08:50:11, 18.7 KB) [[attachment:CustomCheckBox.py]]
  • [get | view] (2009-11-30 08:52:03, 9.6 KB) [[attachment:CustomCheckBox.zip]]
  • [get | view] (2009-11-30 08:50:15, 8.8 KB) [[attachment:CustomCheckBoxDemo.py]]
  • [get | view] (2011-09-02 03:55:29, 14.1 KB) [[attachment:CustomCheckBoxMod_Demo.zip]]
  • [get | view] (2009-11-30 08:50:06, 1.4 KB) [[attachment:checked.ico]]
  • [get | view] (2009-11-30 08:50:17, 1.4 KB) [[attachment:notchecked.ico]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.