You might think that a wxTextCtrl is a simple control, but if you think that, you are very wrong. The wxTextCtrl contains nearly fifty methods. Note that I cannot cover that amount of methods in this article while keeping your interest, so don't expect me to. Instead, I'll just cover the more interesting ones. First, let's build an application for creating a wxTextCtrl. We'll use our knowledge from the last article to center the control, and we'll also throw in a label: from wxPython.wx import * class OurFrame ( wxFrame ): def __init__ ( self ): wxFrame.__init__ ( self, None, -1, 'wxPython' ) self.panel = wxPanel ( self, -1 ) self.text = wxTextCtrl ( self.panel, -1 ) self.vertical = wxBoxSizer ( wxVERTICAL ) self.vertical.Add ( ( 0, 0 ), 1, wxEXPAND ) self.vertical.Add ( wxStaticText ( self.panel, wxID_ANY, 'A Simple wxTextCtrl:' ), 0, wxALIGN_CENTER ) self.vertical.Add ( self.text, 0, wxALIGN_CENTER ) self.vertical.Add ( ( 0, 0 ), 1, wxEXPAND ) self.horizontal = wxBoxSizer ( wxHORIZONTAL ) self.horizontal.Add ( ( 0, 0 ), 1, wxEXPAND ) self.horizontal.Add ( self.vertical, 0, wxALIGN_CENTER ) self.horizontal.Add ( ( 0, 0 ), 1, wxEXPAND ) self.panel.SetSizerAndFit ( self.horizontal ) self.Show ( True ) application = wxPySimpleApp() window = OurFrame() application.MainLoop() When I was learning HTML, one of the things I found interesting was aligning text in a text box. Text can also be aligned in a wxTextCtrl using wxTE_LEFT, wxTE_CENTRE and wxTE_RIGHT: from wxPython.wx import * class OurFrame ( wxFrame ): def __init__ ( self ): wxFrame.__init__ ( self, None, -1, 'wxPython' ) self.panel = wxPanel ( self, -1 ) # Create a left-aligned text box ( default behavior ) self.text = wxTextCtrl ( self.panel, -1 ) # Create a centered text box self.center = wxTextCtrl ( self.panel, -1, style = wxTE_CENTRE )
# Create a right-aligned text box self.right = wxTextCtrl ( self.panel, -1, style = wxTE_RIGHT ) self.vertical = wxBoxSizer ( wxVERTICAL ) self.vertical.Add ( ( 0, 0 ), 1, wxEXPAND ) self.vertical.Add ( wxStaticText ( self.panel, wxID_ANY, 'A Simple wxTextCtrl:' ), 0, wxALIGN_CENTER ) # Add everything self.vertical.Add ( self.text, 0, wxALIGN_CENTER ) self.vertical.Add ( self.center, 0, wxALIGN_CENTER ) self.vertical.Add ( self.right, 0, wxALIGN_CENTER ) self.vertical.Add ( ( 0, 0 ), 1, wxEXPAND ) self.horizontal = wxBoxSizer ( wxHORIZONTAL ) self.horizontal.Add ( ( 0, 0 ), 1, wxEXPAND ) self.horizontal.Add ( self.vertical, 0, wxALIGN_CENTER ) self.horizontal.Add ( ( 0, 0 ), 1, wxEXPAND ) self.panel.SetSizerAndFit ( self.horizontal ) self.Show ( True ) application = wxPySimpleApp() window = OurFrame() application.MainLoop()
blog comments powered by Disqus |
|
|
|
|
|
|
|