Let us now familiarize a new tool kit - wxPython for Python, which will help us to develop GUIs in Python. wxPython is a free cross platform toolkit for developing GUI applications in Python. First of all, download and install wxPython tool kit according to your operating system, and Python version. All versions are available in the domain - http://wxpython.org/
After finishing the installation you can check whether your PC is equipped with wxPython, by importing the module. For doing your first program using wxPython, try the following code in your favorite editor and run it
import wx class myGUI(wx.App): def OnInit(self): frame = wx.Frame(parent=None, title=’Python GUI’) frame.Show() return True app = myGUI() app.MainLoop()
Now let us analyze the program.
import wx, imports all modules in wxPython. Here wx is a namespace for the toolkit. All functions and objects will start with the prefix “wx.”. wxPython has a collection of widgets. One of the important widget is wx.Frame, which we will use to create an application. For this, we define wx. App class as -‘myGUI’.
Here we have used wx.App with OnInit() method for simplicity. Elements of python GUI is defined under definition of OnInt(). Here we have defined a frame with title ’Python GUI’. To show the frame on screen we have used frame.Show().
app.MainLoop() works like an infinite loop, which will wait until a message comes in or the user closes the window.
There is one more method to create a GUI frame in Python. It is __init__ (double underscore at the start and end of init). The __init__ method is used to initialize the new instance and it is similar to a constructor in C++ or Java. The __init__ method not only defines the class but also pass arguments when a new instance is created. So in the example cited earlier we have used wx.App with OnInit() method. Now let us see the same program using wx.Frame with __init__
import wx class Example(wx.Frame): def __init__(self,parent,id): wx.Frame.__init__(self,parent,id,'Python GUI') self.panel=wx.Panel(self) app=wx.PySimpleApp() frame=Example(parent=None,id=-1) frame.Show() app.MainLoop()
Now try the following program which creates a GUI of 500x 500 pixels with one text box and two buttons. When you enter your name in the box and press ‘ENTER’ button, your name will be printed in the IDLE. The program can be modified to manipulate the string that gets printed in the IDLE, if you manipulate the definition block in the code.
import wx class Example(wx.Frame): def __init__(self,parent,id):#initialization, size of window can be given here wx.Frame.__init__(self,parent,id,'My First Python GUI', size=(500,500)) self.panel=wx.Panel(self) # the following code gives the definition of label and text box Label=wx.StaticText(self.panel, -1, "Type your Name: ", pos=(80,150)) self.name=wx.TextCtrl(self.panel, -1, "Name", size=(200, 20), pos=(180,150)) self.name.SetInsertionPoint(0) # Definition of Enter button follows Enter=wx.Button(self.panel, -1, "Enter", pos=(120,200), size=(120,30)) self.Bind(wx.EVT_BUTTON, self.Result, Enter) # Definition of Exit button follows Exit=wx.Button(self.panel, -1, "Quit", pos=(250,200), size=(50,30)) self.panel.Bind(wx.EVT_BUTTON, self.Exitbutton, Exit) # Action of Enter button def Result(self, panel): Name= self.name.GetValue() print Name # Action of Exit button def Exitbutton(self, panel): self.Close(True) # loop controls and to show frames app=wx.PySimpleApp() frame=Example(parent=None,id=-1) frame.Show() app.MainLoop()