Home arrow Python arrow Page 6 - A Look at wxPython

Some Widgets - Python

This article is the first in a series covering wxPython, a library that makes it easy for Python programmers to build graphical user interfaces. It introduces some simple dialogs, explains how to create a simple frame, how to create a menu, and how to add two basic controls to a panel.

TABLE OF CONTENTS:
  1. A Look at wxPython
  2. The Basics
  3. Creating Windows and Status Bars
  4. Creating Menus
  5. Events
  6. Some Widgets
By: Peyton McCullough
Rating: starstarstarstarstar / 23
June 22, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More Python Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: