Page 1 of 1

Introduction to Python and wxPython

#1 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,132
  • Joined: 07-September 06

Post icon  Posted 07 June 2008 - 02:26 PM

An Important thing to know about Python

Python is an interesting language in the amount that you can do with it and the speed at which an application can be put together. You can literally walk in, pick up python and have a working application within a few hours of installation. That is all fine and dandy, but there is a darker side to python. As I state later on, Python applications can be SLOW and I mean it! The reason for this is that Python is a “compile as you go” type of language. This means that each line of code is compiled in order and each time a loop is processed it is re-compiled with the different variables. Because of this you can have a small application of less than 100 lines and receive an error on line 40102 (which obviously doesn’t exist in your code). It does exist when the application is looped over and over hundreds of times and just continually compiled like one large program.

Before we start

Today we will be looking at a different language than normal, but just as useful and a fast learn. Starting off we will be creating a Hello World application that will help you understand how to work with Python.

Starting off make sure that you have the required language installed on your home computer.
To download Python:
http://www.python.org
To download wxPython:
http://www.wxpython.org

Now that you have completed that initial portion of this tutorial we will move on to the code.

Hello World application (code explained)

Because wxPython works wonders when it comes to creating gui elements and just being an easy to use framework, we will br creating our Hello World application with it. Start off by opening your favorite editor (I recommend eclipse: http://www.eclipse.org ).

The first thing to do is import the wxPython modules so that we can use them in our application, that can be achieved through this line of code:

import wx


Now that it complete and it was easy wasn’t it? Moving on we will need to create out frame class, which as you may expect will be responsible for the main frame of the application. We can declare this class with the following line of code:

class MyFrame(wx.Frame):


At this point in time you have probably noticed that python is different from many other programming languages out there. If not, we will explain. Because python is made for speed (of programming, not necessarily of execution) the language has done away with the needs (in most cases) for brackets ({, }) and semi-colons (;).

Looking ahead we will need to create an init function, which is the first function run in any and all python classes. It can be created like so:
	def __init__(self, parent, id, title):


At this point you have probably noticed something else that it peculiar, python uses definitions instead of functions, this is again for speed. Start all functions with def instead. Also notice that python’s content is tab oriented (I understand it is hard to see on the forums, which tend to do away with tabs and additional spaces, but there is a tab there).
I understand that you are eager to move along with learning how to work with python, so we will move along to the creation of our frame:

		wx.Frame.__init__(self, parent, id, title, size=(400, 200))


The above line of code could be the last line we need for our frame, but we decided to go a little bit further (later on we also add a status bar into the mix). Basically the above line is calling to wxPython’s library Frame and initiating it (sending the variables inside the ()s to the function to get things moving).

This next statement is used to center the newly created window on the screen. I tlooks like so:
		self.CenterOnScreen()


Basically “self” is calling to the class, and since this class is an alias for the wx.Frame class it will be telling the frame to position itself in the center of the screen.

		self.CreateStatusBar()


The line of code above is again telling the frame (self) to create a status bar, which is placed at the bottom of the window. That, combined with the next line of code tells the newly made status bar to display the text “Status bar”.

		self.SetStatusText("Status bar")


Simply close off the class (for speed and convenience python simply requires that you outdent all the indents that you have made and the class closes itself). Now we have completed creating the Frame, and need to initiate it (from a handler class) again wxPython has been kind enough to add speed to the process.

class MyApp(wx.App):


Above is yet another class (which is an alias for wx.App class) called MyApp.

	def OnInit(self):[code]

This is an odd instance, we will not be using the normal __init__() definition for this init function. Because this is the first function being called in the whole application, it is required to be named OnInit. This is so python can tell that it is the function which is supposed to be run first in the application, thought the class still needs to be pointed out for python to look in the right place.

[code]		frame = MyFrame(None, -1, 'Hello World')


With the code above we point to the MyFrame class (alias for wx.Frame class) and send some parameters of (None, -1, ‘Hello World’). These parameters aren’t just for show, and as python likes to offer many ways of becoming object oriented the first parameter acts as the new class’ parent, None of course sets the parent as nothing. Then we send -1, which gives the called class (MyFrame) the id of -1 (basically stating it doesn’t matter on what the id is). The third and final variable sent is what we are using as the frame (window) title; it will display “Hello World”. We also set this class to a variable (frame) to allow for us to call it at a future time, from other functions and classes called by MyApp, that allows the class to have a parent (instead of sending None, send something like self).

		frame.Show(True)


We make our first call to the MyFrame class from MyApp by stating frame.Show(true) (the code above). This tells the wx.Frame to display itself if it isn’t already. Likewise setting the True to False will cause the window to hide, or not display.

		self.SetTopWindow(frame)


This code tells the application to make frame (MyFrame) the top window on the screen.

		return True


The return tells MyApp to return true, which it does as long as a fatal error doesn’t happen and nobody tells the window to close.

app = MyApp(0)
app.MainLoop()


Finally we have what sets the whole application into motion. The base calls. App is a variable set as a pointer to MyApp (the wx.App alias class). And the next line just says that the app (MyApp, wx.App) class is the Main Loop and to continue running through the loop until the program dies or is closed.

The full code looks like so:
import wx

class MyFrame(wx.Frame):
	def __init__(self, parent, id, title):
		wx.Frame.__init__(self, parent, id, title, size=(400, 200))
		self.CenterOnScreen()
		self.CreateStatusBar()
		self.SetStatusText("Status bar")

class MyApp(wx.App):
	def OnInit(self):
		frame = MyFrame(None, -1, 'Hello World')
		frame.Show(True)
		self.SetTopWindow(frame)
		return True

app = MyApp(0)
app.MainLoop()


Congratulations on creating your first Python wxPython application!

If you are using eclipse save your code off (is you haven’t already) and press the yellow arrow to run it. If you are using another editor, or notepad you will need to save the application and open it with python (save the file as a .py file and then double click on it to run).

Wrapping it up

This application is very simple, I know, but it is still a fundamental part of learning a new language. From here you can do interesting and fun things with your application to make it more complex and eventually something that is useful to the world. If you want to mess around with the code feel free to, but make a copy of it first as to not accidentally destroy something you cared about.

Here are some functions that you can start playing with before the next tutorial in thi series comes out:
SetTitle() and SetSize()
NOTE - SetSize() takes a tuple, or two variables combined into what is commonly known as an array, basically like so: SetSize([0, 0])

Have fun, and look forward to the next installment of this tutorial series.

Is This A Good Question/Topic? 3

Replies To: Introduction to Python and wxPython

#2 H_J_89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-August 09

Posted 13 August 2009 - 08:58 AM

IT can be slow but there R ways to eliminate that like using threading >p
Was This Post Helpful? 0
  • +
  • -

#3 CMIT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-February 12

Posted 26 February 2012 - 01:20 AM

Nice is there any place where one can get a complete list of all the stuff in a given module like wxPython? I'm sorta interested in being able to display images with Python at some point. I've got some experience with logic and programming but I've not been using it much and I'm new to python so I've started with the basics.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1