Blogroll

Forums

python

Creating a GUI in Python using Tkinter

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.

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:

[code lang=”python”]
from Tkinter import *
[/code]

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:

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 HelloWorldlabel 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.

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:

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.

“””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)

Hai,
your website tutorials are really good. Could you please tell me where I can find a complete tutorials to Tkinter. It would be quite helpful for me as i am a beginner in GUI building using python.

I am learning Python through “Think Python an Intro to software design” by Allen. I am now in Chapter 3. This book is based on v2.* and there is already a v3. However, the book has footnotes regarding some changes in v2, so I decided to use IDLE for v3.

Since I am interested in GUI programming in Python, I was disappointed when Example One code did not run in IDLE v3. I need your advise whether to use v2 of IDLE and learn later the v3.

I ran the Hello World GUI example (in IDLE3) and it worked fine but when I closed it the window disappeared but I didn’t get a new prompt in the python shell and when I tried to run it again I got a popup warning me that there was a script still running plus the following text in the python shell.

“Note that you should not run these programs under IDLE. IDLE is itself a Tkinter application, with its own “mainloop” that will conflict with the mainloop in these programs. If you really want to view and run these programs using IDLE, then — for each program — you should comment out the “mainloop” statement in the program before running it.”

My name is Wayne Reuvers, and I can make your website UNHACKABLE, GUARANTEED.
Do not put yourself at risk.
Call 1-845-351-5100 to contact a representative. Email me at wreuvers@livetechnology.com for
a live demonstration.
FREE CHALLENGE: If you are able to hack our website (livetechnology.com) OUR
SERVICES ARE FREE! Do not waste another moment. Call
us now. ?? ???? ?? !