EyeDropper

Simple Eye Dropper tool sample using a ScreenDC.

NOTE: will not work on wxMac due to current limitations of the ScreenDC buffer being write only!.

Code

   1 # Simple ScreenDC example code to make a color eyedropper tool
   2 # Author: Cody Precord
   3 import wx
   4 
   5 class EyeDropperFrame(wx.Frame):
   6     """Simple Eyedropper toy"""
   7     def __init__(self, parent, title=u""):
   8         wx.Frame.__init__(self, parent, title=title)
   9 
  10         # Attributes
  11         self.panel = EyeDroppwerPanel(self)
  12 
  13         # Layout
  14         self.__DoLayout()
  15 
  16     def __DoLayout(self):
  17         sizer = wx.BoxSizer()
  18         sizer.Add(self.panel, 1, wx.EXPAND)
  19         self.SetSizer(sizer)
  20         self.SetInitialSize((200, 200))
  21 
  22 class EyeDroppwerPanel(wx.Panel):
  23     def __init__(self, parent):
  24         wx.Panel.__init__(self, parent)
  25 
  26         # Attributes
  27         self.palette = PreviewPalette(self, size=(100, 100))
  28 
  29         # Layout
  30         self.__DoLayout()
  31 
  32     def __DoLayout(self):
  33         hsizer = wx.BoxSizer(wx.HORIZONTAL)
  34         vsizer = wx.BoxSizer(wx.VERTICAL)
  35         vsizer.AddStretchSpacer()
  36         vsizer.Add(self.palette, 0, wx.ALL | wx.EXPAND, 30)
  37         vsizer.AddStretchSpacer()
  38         hsizer.AddStretchSpacer()
  39         hsizer.Add(vsizer, 0, wx.EXPAND)
  40         hsizer.AddStretchSpacer()
  41         self.SetSizer(hsizer)
  42         
  43 class PreviewPalette(wx.Panel):
  44     """Panel to display the color sampleing from the eyedropper"""
  45     def __init__(self, parent, id=wx.ID_ANY,
  46                  pos=wx.DefaultPosition, size=wx.DefaultSize):
  47         wx.Panel.__init__(self, parent, id, pos, size)
  48 
  49         # Attributes
  50         self.color = wx.BLACK
  51         self.brush = wx.Brush(self.color)
  52         self.timer = wx.Timer(self)
  53 
  54         # Event Handlers
  55         self.Bind(wx.EVT_PAINT, self.OnPaint)
  56         self.Bind(wx.EVT_TIMER, self.OnTimer)
  57         self.timer.Start(250)
  58 
  59     def __del__(self):
  60         if self.timer.IsRunning():
  61             self.timer.Stop()
  62 
  63     def OnPaint(self, evt):
  64         dc = wx.PaintDC(self)
  65         dc.SetBrush(self.brush)
  66         rect = self.GetClientRect()
  67 
  68         # Draw the preview square
  69         sq_x = (rect.width / 2) - 25
  70         sq_y = (rect.height / 2) - 25
  71         dc.DrawRectangle(sq_x, sq_y, 50, 50)
  72 
  73         hexcode = self.color.GetAsString(wx.C2S_HTML_SYNTAX)
  74         tsize = self.GetTextExtent(hexcode)
  75         lbl_rect = wx.Rect(sq_x - 20, sq_y + 55, 90, 20)
  76         print lbl_rect
  77         dc.SetPen(wx.BLACK_PEN)
  78         dc.SetFont(self.GetFont())
  79         dc.DrawLabel(hexcode, lbl_rect, wx.ALIGN_CENTER)
  80 
  81     def OnTimer(self, evt):
  82         pos = wx.GetMousePosition()
  83         dc = wx.ScreenDC()
  84         color = dc.GetPixelPoint(pos)
  85         if color != self.color:
  86             self.color = color
  87             self.brush.SetColour(self.color)
  88             self.Refresh()
  89 
  90 if __name__ == '__main__':
  91     app = wx.App(False)
  92     frame = EyeDropperFrame(None, title="Eyedropper Toy")
  93     frame.Show()
  94     app.MainLoop()

EyeDropper (last edited 2009-08-13 20:03:04 by 12)

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