Skip to content
Dharmatej Mikkilineni edited this page Jun 20, 2013 · 3 revisions

This tutorial will explain how to create a simple GUI in Python using the wxPython wrapper, so that the different features in oscaarGUI.py can be better understood. To create the GUI in Python we will use an object oriented approach with other features interwoven. You will need to have Python 2.7 installed on your computer. You can download it here.

First start by opening a blank text file in any type of text editor. Common ones are Emacs, Notepad, Notepad++, TextEdit, Eclipse, etc... Enter the following code and make sure the spacing is correct.

import  wx

# This is a comment.

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1)

if __name__ == "__main__":
    app = wx.App()
    MainFrame().Show()
    app.MainLoop()

Save the file as simpleGUI.py, where .py is the extension for a python source file. Then open up the command prompt or terminal, and switch to the directory with your file in it using the cd command. If you have Python installed correctly, running the command python simpleGUI.py will create your first blank GUI. Now lets take a look at what each line did.

The import wx command will allow us to use the wx framework for creating frames and panels. The next line has a #, which in python is the start of a [comment](http://en.wikipedia.org/wiki/Comment_(computer_programming\)). Class is a [keyword](http://en.wikipedia.org/wiki/Keyword_(computer_programming\)) in Python, and will allow us to define the frame that we will use, MainFrame. The parameter wx.Frame is passed in so that we can inherit from the base class for standard windows. Any method you create inside of a class must have an indent because in Python spacing is key in defining the [scope](http://en.wikipedia.org/wiki/Scope_(computer_science\)) of the code. Next we must make the constructor, which actually creates the frame when any [instance](http://en.wikipedia.org/wiki/Instance_(computer_science\)) of the class is called. This is the first method you will have to always write. To define a method you use the keyword def put a space and write the name of your method, with the input parameters you would like. Here self is another keyword that defines the class that you are in. This method should always be named __init__ with at least the parameter self (more advanced users will put other parameters). The : must follow because that is Python syntax. The first line of the __init__ method calls the wx framework. The three parameters are self, parent, and ID. For most simple GUI's the defaults should be None for the parent, and -1 for the ID. After that, we need to write a section of code that will run the main program. When using wxPython, it is mandatory to create an instance of wx.App() before creating GUI elements. We create the MainFrame, and then use the last line to loop indefinitely. The reason to loop is so that the program will wait for mouse clicks, key presses, and other user input.

Now that we have that done, we can make a This is still being updated, it is not done yet.

Clone this wiki locally