A Look at wxPython - Some Widgets
(Page 6 of 6 )
Now let's add some controls to our window. First, though, let's shift the structure of our application. Instead of throwing everything in the global namespace, let's create a class –- a subclass of wxFrame. While we're add it, how about setting the size of our window?
from wxPython.wx import *
class Window ( wxFrame ):
def __init__ ( self ):
wxFrame.__init__ ( self, None, wxID_ANY, 'Title', size = ( 350, 350 ) )
self.Show ( True )
application = wxPySimpleApp()
window = Window()
application.MainLoop()
Now we're ready. Let's add a simple button to our window:
from wxPython.wx import *
class Window ( wxFrame ):
def __init__ ( self ):
wxFrame.__init__ ( self, None, wxID_ANY, 'Title', size = ( 350, 350 ) )
self.button = wxButton ( self, 100, 'Click' )
self.Show ( True )
application = wxPySimpleApp()
window = Window()
application.MainLoop()
Ugh, it takes up the whole window. This can be fixed by putting it in a panel:
from wxPython.wx import *
class Window ( wxFrame ):
def __init__ ( self ):
wxFrame.__init__ ( self, None, wxID_ANY, 'Title', size = ( 350, 350 ) )
self.panel = wxPanel ( self, wxID_ANY )
self.button = wxButton ( self.panel, 100, 'Click' )
self.Show ( True )
application = wxPySimpleApp()
window = Window()
application.MainLoop()
That's better. Now let's add a text box:
from wxPython.wx import *
class Window ( wxFrame ):
def __init__ ( self ):
wxFrame.__init__ ( self, None, wxID_ANY, 'Title', size = ( 350, 350 ) )
self.panel = wxPanel ( self, wxID_ANY )
self.button = wxButton ( self.panel, 100, 'Click' )
self.text = wxTextCtrl ( self.panel, 101, 'Some text.', ( 100, 100 ) )
self.Show ( True )
application = wxPySimpleApp()
window = Window()
application.MainLoop()
Now let's make our button do something. I won't get into detail about this, since it's pretty simple to understand:
from wxPython.wx import *
class Window ( wxFrame ):
def __init__ ( self ):
wxFrame.__init__ ( self, None, wxID_ANY, 'Title', size = ( 350, 350 ) )
self.panel = wxPanel ( self, wxID_ANY )
self.button = wxButton ( self.panel, 100, 'Click' )
self.text = wxTextCtrl ( self.panel, 101, 'Some text.', ( 100, 100 ) )
EVT_BUTTON ( self.panel, 100, self.click )
self.Show ( True )
def click ( self, event ):
message= wxMessageDialog ( self, 'You clicked the button.', 'Clicked', wxICON_EXCLAMATION )
message.ShowModal()
message.Destroy()
application = wxPySimpleApp()
window = Window()
application.MainLoop()
Wrapping Up
So far, you've learned a little bit about wxPython. You know how to create two simple dialogs, how to create a simple frame, how to create a menu and how to add two basic controls to a panel. That is all I'll explain in this article, but wxPython is a very large library, so don't think that this is it. Dozens of controls, dialogs and frames can be used in your applications –- way more than I can cover in a single article. However, stay tuned. This is definitely not the end.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |