How to create a text control with icon (Phoenix)

Keywords : Text control, icon.


Introduction :

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.


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


Sample one

img_sample_one.png

   1 # sample_one.py
   2 
   3 import wx
   4 
   5 # class MyIconTextCtrl
   6 # class MyFrame
   7 # class MyPanel
   8 # class MyApp
   9 
  10 #---------------------------------------------------------------------------
  11 
  12 class MyIconTextCtrl(wx.Panel):
  13     """
  14     MyIconTextCtrl.
  15     """
  16     def __init__(self, parent, pos=(50, 30), size=(200, 21)):
  17         """
  18         Create the MyIconTextCtrl.
  19         """        
  20         # Create a panel, which looks like a textctrl.
  21         wx.Panel.__init__(self, parent, pos=pos, size=size, style=wx.BORDER_SIMPLE)
  22 
  23         self.SetBackgroundColour("#c1e9ff")
  24 
  25         self.w, self.h = self.GetSize()
  26 
  27         #------------
  28         
  29         # Add a bitmap on the left side.
  30         bmp = wx.ArtProvider.GetBitmap(wx.ART_FIND, wx.ART_OTHER, (16, 16))
  31 
  32         self.staticbmp = wx.StaticBitmap(self, -1, bmp, pos=(1, 1))
  33         w, h = self.staticbmp.GetSize()
  34 
  35         # Create an invisible textctrl inside the textctrl pane.
  36         self.textctrl = wx.TextCtrl(self,
  37                                     pos=(w + 5, 2),
  38                                     size=(self.w - w - 8, h), 
  39                                     style=wx.NO_BORDER |
  40                                           wx.TE_PROCESS_ENTER)
  41         self.textctrl.SetBackgroundColour("#c1e9ff")
  42         self.textctrl.Bind(wx.EVT_TEXT_ENTER, self.OnDisplayText)
  43 
  44     #-----------------------------------------------------------------------
  45         
  46     def OnDisplayText(self, event):
  47         """
  48         Confirm user entered text.
  49         """
  50         
  51         wx.MessageBox("You entered : '" + self.textctrl.GetValue() + "'", "Info")
  52 
  53 #---------------------------------------------------------------------------
  54         
  55 class MyFrame(wx.Frame):
  56     """
  57     TextCtrlWithImage application frame.
  58     """
  59     def __init__ (self, parent, title):
  60         """
  61         Create the TextCtrlWithImage application frame.
  62         """
  63         wx.Frame.__init__(self, parent, title=title)
  64 
  65         #------------
  66         
  67         self.SetSize((340, 150))
  68         self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))
  69         self.SetBackgroundColour(wx.WHITE)
  70         
  71 #---------------------------------------------------------------------------
  72         
  73 class MyPanel(wx.Panel):
  74     """
  75     TextCtrlWithImage Panel.
  76     """
  77     def __init__ (self, parent):
  78         """
  79         Create the TextCtrlWithImage panel.
  80         """
  81         wx.Panel.__init__(self, parent)
  82 
  83         #------------
  84         
  85         MyIconTextCtrl(self)
  86 
  87 #---------------------------------------------------------------------------
  88         
  89 class MyApp(wx.App):
  90     """
  91     TextCtrlWithImage Application.
  92     """
  93     def OnInit(self):
  94         """
  95         Create the TextCtrlWithImage application.
  96         """
  97         
  98         frame = MyFrame(None, title="wx.TextCtrl (with icon)")
  99         panel = MyPanel(frame)
 100         frame.Show(True)
 101         
 102         return True
 103 
 104 #---------------------------------------------------------------------------
 105     
 106 if __name__ == '__main__':
 107     app = MyApp(redirect=False)
 108     app.MainLoop()


Download source

source.zip


Additional Information

Link :

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

??? (sample_one.py coding), the wxPython community...


About this page

Date(d/m/y) Person (bot) Comments :

18/01/21 - Ecco (Created page for wxPython Phoenix).


Comments

- blah, blah, blah....

How to create a text control with icon (Phoenix) (last edited 2021-01-17 15:57:13 by Ecco)

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