Up until now we’ve focused on python applications that only run in the command line. For a lot of tasks this is great, but in order to be really impressive we’re going to want to create a GUI for some of our python applications.
There are a lot of python GUI toolkits out there, but for our first application we are going to use Tkinter, which is considered somewhat of a standard right now and is installed when pyhon is installed. Another nice thing about the Tkinter GUI is that it is cross platform and offers a native look and feel on *nix, Windows, and OS X.
Tkinter Example One
So lets start off with a simply “Hello World” GUI app:
#! /usr/bin/env python from Tkinter import * class GUIFramework(Frame): """This is the GUI""" def __init__(self,master=None): """Initialize""" """Initialize the base class""" Frame.__init__(self,master) """Display the main window""" self.grid() """Create the Text""" self.HelloLabel = Label(master, text="Hello World!") self.HelloLabel.grid() if __name__ == "__main__": guiFrame = GUIFramework() guiFrame.mainloop()
Running this on OS X results in the following:
And running it in Windows Xp gives you:
As you can see the code used to create the GUI is pretty simple and easy to understand, but it is actually more complicated then need be because it uses classes. But since I’ll be using classes whenever building a GUI I thought that this was the best starting point. We start off by importing the Tkinter library into our program using this following:
from Tkinter import *
After that we create a class called GUIFramework, which is the class that will be used to great the GUI elements in Tkinter and uses the Tkinter frame class as its base. For now all the work that we do is done in the __init__ function.
There we first initialize the base class (which will initialize Tkinter) and then tell ourselves to be visible:
"""Initialise the base class""" Frame.__init__(self,master) """Display the main window""" self.grid()
To display ourselves we use the grid geometry manager, the grid geometry manager is a straight forward way to display widgets and position them. Since we have no other widgets in this example the defaults are fine, take a look at example two for for information on widget placement using the grid function.
Next is the process of creating the HelloWorld label widget, which is a member of the GUIFramework class, and setting it to be visible. It’s a good idea to make your widgets members of the class in case you need to access them later, but if you’re sure that you’ll never have to access them later, you can just create them locally.
self.HelloLabel = Label(master, text="Hello World!") self.HelloLabel.grid()
After that all the other processing is done in the “main” section of our code where we initialize our class and tell it to enter the “mainloop”. The “mainloop” is basically the application responding to events:
if __name__ == "__main__": guiFrame = GUIFramework() guiFrame.mainloop()
Tkinter Example Two
Next I’ll show you a more complicated example, I won’t go into specifics with this one since it’s still pretty simple. But if you have any questions about the following code feel free to ask via a comment.
#! /usr/bin/env python from Tkinter import * import tkMessageBox class GUIFramework(Frame): """This is the GUI""" def __init__(self,master=None): """Initialize yourself""" """Initialise the base class""" Frame.__init__(self,master) """Set the Window Title""" self.master.title("Type Some Text") """Display the main window" with a little bit of padding""" self.grid(padx=10,pady=10) self.CreateWidgets() def CreateWidgets(self): """Create all the widgets that we need""" """Create the Text""" self.lbText = Label(self, text="Enter Text:") self.lbText.grid(row=0, column=0) """Create the Entry, set it to be a bit wider""" self.enText = Entry(self) self.enText.grid(row=0, column=1, columnspan=3) """Create the Button, set the text and the command that will be called when the button is clicked""" self.btnDisplay = Button(self, text="Display!", command=self.Display) self.btnDisplay.grid(row=0, column=4) def Display(self): """Called when btnDisplay is clicked, displays the contents of self.enText""" tkMessageBox.showinfo("Text", "You typed: %s" % self.enText.get()) if __name__ == "__main__": guiFrame = GUIFramework() guiFrame.mainloop()
This creates the following window:
That shows this dialog when the button is pressed:
Links that helped me get through this post:
If you like this post remember to digg it.