This is a simple extension of TextCtrl.

I will display an icon on the left side. For example useful in an instant messenger application.

It consists of: A panel, which looks like a wx.TextCtrl, an icon and an invisible wx.TextCtrl.

Screenshot

textctrlwithicon.png

Code

   1 #!/usr/bin/env python
   2 
   3 import wx
   4 
   5 class MyIconTextCtrl(wx.Panel):
   6     """MyIconTextCtrl."""
   7 
   8     def __init__(self, parent, pos=(50,50), size=(100, 21)):
   9         """Create the MyIconTextCtrl."""
  10         #create a panel, which looks like a textctrl
  11         wx.Panel.__init__(self, parent, pos=pos, size=size, style=wx.SUNKEN_BORDER)
  12         self.SetBackgroundColour(wx.WHITE)
  13         self.w, self.h = self.GetSize()
  14 
  15         #add a bitmap on the left side
  16         bmp = wx.ArtProvider_GetBitmap(wx.ART_FIND, wx.ART_OTHER, (16, 16))
  17         self.staticbmp = wx.StaticBitmap(self, -1, bmp, pos=(1, 0))
  18         w, h = self.staticbmp.GetSize()
  19 
  20         #create an invisible textctrl inside the textctrl pane.
  21         self.textctrl = wx.TextCtrl(self, pos=(w + 5, 2), size=(self.w - w - 8, h), 
  22                                     style=wx.NO_BORDER)
  23         self.textctrl.Bind(wx.EVT_TEXT_ENTER, self.OnDisplayText)
  24 
  25     def OnDisplayText(self, event):
  26         """Confirm user entered text."""
  27         wx.MessageBox("You Entered: '" + self.textctrl.GetValue() + "'", "Info")
  28 
  29 class MyFrame(wx.Frame):
  30     """TextCtrlWithImage Application Frame."""
  31     def __init__ (self, parent, title):
  32         """Create the TextCtrlWithImage Application Frame."""
  33         wx.Frame.__init__(self, parent, title=title)
  34 
  35 class MyPanel(wx.Panel):
  36     """TextCtrlWithImage Panel."""
  37     def __init__ (self, parent):
  38         """Create the TextCtrlWithImage Panel."""
  39         wx.Panel.__init__(self, parent)
  40         MyIconTextCtrl(self)
  41 
  42 class MyApp(wx.App):
  43     """TextCtrlWithImage Application."""
  44     def OnInit(self):
  45         """Create the TextCtrlWithImage Application."""
  46         frame = MyFrame(None, title="Test: TextCtrl with Image")
  47         panel = MyPanel(frame)
  48         frame.Show(True)
  49         return True
  50 
  51 if __name__ == '__main__':
  52     app = MyApp(redirect=False)
  53     app.MainLoop()

TextCtrlWithIcon (last edited 2008-03-11 10:50:31 by localhost)

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