1. Summary
1. Constructions of four common edit boxes
1. Normal Edit Box
Simple edit box with one-line input (? For the object in the box, the same below)
self.textBox = wx.TextCtrl(parent = ?)
2. Password edit box
Asterisk masks the password box of the input character
self.textBox = wx.TextCtrl(parent = ?, style = wx.TE_PASSWORD)
3. Read-only Text Box
Read-only text boxes that cannot be changed but can be copied
Self.textBox = wx.TextCtrl (parent =?, style = wx.TE_READONLY, value = "I am a read-only text box")
When we want to modify the content, we can: self.TextBox.SetValue("String")
4. Multiline Text Box
Text boxes that can be entered on multiple lines (single lines longer than no line wrap)
self.textBox =wx.TextCtrl(parent = ?,style = wx.TE_MULTILINE)
(Single line exceeds Wrap, i.e. horizontal scrollbars)
self.textBox =wx.TextCtrl(parent = ?,style = wx.TE_MULTILINE|wx.HSCROLL
2. Event Binding
Self.textBox.Bind (event type, corresponding function)
Common event types are:
3. Styles in edit box construction
The constructor form of the TextCtrl class is as follows:
wx.TextCtrl(parent, id, value, pos, size, style)
value: Text in the edit box
pos: The position of the edit box in the window
Size: the size of the edit box
Style: the style parameters of the edit box
Common parameters accepted by style
2. Feeling
The edit box component itself is simple, but it is generally used in conjunction with the layout manager (wx.BoxSizer class) and the label (wx.StaticText class)
Currently, only a few of the commonly used parameters are listed here. For example, style has many more parameters to accept
There are no functions to modify the contents of the edit box at this time, as they are not commonly used and will be updated later.
Recommend a more complete information: http://justcoding.iteye.com/blog/914125
3. Operation effect
4. Sample Code
PS: This code reflects the common content of the edit box (what we're talking about in this article) and the simple use of layout manager (wx.BoxSizer class) and labels (wx.StaticText class)
1 #coding:utf-8 2 #author:Twobox 3 4 import wx 5 6 class Mywin(wx.Frame): 7 def __init__(self, parent, title): 8 super(Mywin, self).__init__(parent, title = title) 9 10 #Create a panel on the window frame 11 panel = wx.Panel(self) 12 13 #Create Vertical Dimension Manager : Used to manage the next Horizontal Manager and other components 14 vbox = wx.BoxSizer(wx.VERTICAL) 15 16 #Create Horizontal Manager hbox1,Label, Normal Edit Box(Binding Content Change Event) , And put the latter two in hbox1 upper 17 hbox1 = wx.BoxSizer(wx.HORIZONTAL) 18 stctext1 = wx.StaticText(panel, id = -1, label = "Text box:") 19 self.t1 = wx.TextCtrl(panel) 20 self.t1.Bind(wx.EVT_TEXT, self.OnKeyTyped) 21 hbox1.Add(stctext1, proportion = 0, flag = wx.EXPAND|wx.ALL, border = 5) 22 hbox1.Add(self.t1, proportion = 1, flag = wx.EXPAND|wx.ALL, border = 5) 23 24 #Create Horizontal Manager hbox2,Label, Password Edit Box(Set to 6 for large length)(Binding Text Length Excess Event) , And put the latter two in hbox2 upper 25 hbox2 = wx.BoxSizer(wx.HORIZONTAL) 26 stcText2 = wx.StaticText(panel, id = -1, label = "Password box:") 27 self.t2 = wx.TextCtrl(panel, style = wx.TE_PASSWORD) 28 self.t2.SetMaxLength(6) 29 self.t2.Bind(wx.EVT_TEXT_MAXLEN, self.OnMaxLen) 30 hbox2.Add(stcText2, proportion = 0, flag = wx.EXPAND|wx.ALL, border = 5) 31 hbox2.Add(self.t2, proportion = 1, flag = wx.EXPAND|wx.ALL, border = 5) 32 33 # Create Horizontal Manager hbox3,Label, read-only edit box, and put both in hbox3 upper 34 hbox3 = wx.BoxSizer(wx.HORIZONTAL) 35 stcText4 = wx.StaticText(panel, id = -1, label = "Read-only box:") 36 self.t4 = wx.TextCtrl(panel, style = wx.TE_READONLY|wx.TE_CENTER, value = "I am a read-only text box") 37 hbox3.Add(stcText4, proportion = 0, flag = wx.EXPAND|wx.ALL, border = 5) 38 hbox3.Add(self.t4, proportion = 1, flag = wx.EXPAND|wx.ALL, border = 5) 39 40 #take hbox1 , hbox2 ,hbox3 add to vbox upper 41 vbox.Add(hbox1, proportion=0, flag=wx.EXPAND | wx.ALL, border=0) 42 vbox.Add(hbox2, proportion=0, flag=wx.EXPAND | wx.ALL, border=0) 43 vbox.Add(hbox3, proportion=0, flag=wx.EXPAND | wx.ALL, border=0) 44 45 #Create labels, multiline text boxes(Bind Press Enter Event) , And put both in vbox upper 46 stcText3 = wx.StaticText(panel, id = -1, label = "Multiline Text Box", style = wx.ALIGN_CENTER) 47 self.t3 = wx.TextCtrl(panel, style = wx.TE_MULTILINE) 48 self.t3.Bind(wx.EVT_TEXT_ENTER, self.OnEnterPressed) 49 vbox.Add(stcText3, proportion = 0, flag = wx.EXPAND|wx.ALL, border = 5) 50 vbox.Add(self.t3, proportion = 1, flag = wx.EXPAND|wx.ALL, border = 5) 51 52 #Settings panel panel The dimension manager for vbox 53 panel.SetSizer(vbox) 54 55 #Adjust window frame and display 56 self.SetSize((350,500)) 57 self.Center() 58 self.Show() 59 self.Fit() 60 61 def OnKeyTyped(self, event): 62 print(event.GetString()) 63 64 def OnEnterPressed(self, event): 65 print("Enter Pressed") 66 67 def OnMaxLen(self, event): 68 print("Maximum length reached") 69 70 def main(): 71 app = wx.App() 72 Mywin(None, "Edit box sample") 73 app.MainLoop() 74 75 if __name__ == '__main__': 76 main()
2017-08-21 17:09:54