In this wxPython article i want to show you How To Create TextControl in wxPython. so a text control allows text to be displayed and edited. It may be single line or multi-line. Notice that a lot of methods of the text controls are found in the base wx.TextEntry class which is a common base class for wx.TextCtrl and other controls using a single line text entry field (e.g. wx.ComboBox).
Also you can check More GUI Development Tutorials in the below link.
1: PyQt5 GUI Development Tutorials
2: TKinter GUI Development Tutorials
3: Pyside2 GUI Development Tutorials
4: Kivy GUI Development Tutorials
Window Styles
This class supports the following styles:
wx.TE_PROCESS_ENTER
: The control will generate the eventwxEVT_TEXT_ENTER
(otherwise pressing Enter key is either processed internally by the control or used to activate the default button of the dialog, if any).wx.TE_PROCESS_TAB
: Normally,TAB
key is used for keyboard navigation and pressing it in a control switches focus to the next one. With this style, this won’t happen and if theTAB
is not otherwise processed (e.g. bywxEVT_CHAR
event handler), a literalTAB
character is inserted into the control. Notice that this style has no effect for single-line text controls when using wxGTK.wx.TE_MULTILINE
: The text control allows multiple lines. If this style is not specified, line break characters should not be used in the controls value.wx.TE_PASSWORD
: The text will be echoed as asterisks.wx.TE_READONLY
: The text will not be user-editable.wx.TE_RICH
: Use rich text control under MSW, this allows having more than64KB
of text in the control. This style is ignored under other platforms.wx.TE_RICH2
: Use rich text control version 2.0 or higher under MSW, this style is ignored under other platformswx.TE_AUTO_URL
: Highlight the URLs and generate the TextUrlEvents when mouse events occur over them.wx.TE_NOHIDESEL
: By default, the Windows text control doesn’t show the selection when it doesn’t have focus – use this style to force it to always show it. It doesn’t do anything under other platforms.wx.TE_LEFT
: The text in the control will be left-justified (default).wx.TE_CENTRE
: The text in the control will be centered (wxMSW, wxGTK, wxOSX).wx.TE_RIGHT
: So The text in the control will be right-justified (wxMSW, wxGTK, wxOSX).
So this is the complete code for How To Create TextControl in wxPython
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import wx class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title, size = (800,600)) panel = MyPanel(self) class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) #self.textCtrl = wx.TextCtrl(self, style = wx.TE_PASSWORD) #self.textCtrl = wx.TextCtrl(self, style = wx.TE_MULTILINE) #self.textCtrl = wx.TextCtrl(self, style = wx.TE_READONLY) self.textCtrl = wx.TextCtrl(self, size = (200,100), style = wx.TE_MULTILINE) class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Text Control") self.frame.Show() return True app = MyApp() app.MainLoop() |
At the top we have created our MyFrame. this class is a top level window that inherits from wx.Frame and we create the object of our MyPanel class in here.
1 2 3 4 5 6 7 |
class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title, size = (800,600)) panel = MyPanel(self) |
After that we have created our MyPanel class that inherits from wx.Panel. and we create our TextCtrl in this class.
1 2 3 4 5 6 7 8 9 10 11 |
class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) #self.textCtrl = wx.TextCtrl(self, style = wx.TE_PASSWORD) #self.textCtrl = wx.TextCtrl(self, style = wx.TE_MULTILINE) #self.textCtrl = wx.TextCtrl(self, style = wx.TE_READONLY) self.textCtrl = wx.TextCtrl(self, size = (200,100), style = wx.TE_MULTILINE) |
These are the different kinds of TextControl in wxPython
1 2 3 4 5 6 |
#self.textCtrl = wx.TextCtrl(self, style = wx.TE_PASSWORD) #self.textCtrl = wx.TextCtrl(self, style = wx.TE_MULTILINE) #self.textCtrl = wx.TextCtrl(self, style = wx.TE_READONLY) self.textCtrl = wx.TextCtrl(self, size = (200,100), style = wx.TE_MULTILINE) |
So the last class is MyApp class that inherits from wx.App. the OnInit() method is where you will most often create frame subclass objects. passes to wxPython. Unlike procedural programs, a wxPython GUI program primarily responds to the events taking place around it, mostly determined by a human user clicking with a mouse and typing at the keyboard. When all the frames in an application have been closed, the app.MainLoop() method will return and the program will exit.
1 2 3 4 5 6 7 8 9 10 |
class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Text Control") self.frame.Show() return True app = MyApp() app.MainLoop() |
So run the code and this will be the result
Also you can watch the complete video for this article