Python27: Building a Simple TKinter GUI

Not too long ago, my job required me to build an interactive GUI with Python 2.7. I was limited to using what comes with the basic Python installation, so that the GUI could be used on any computer, so long as Python was installed. This meant that I had to learn how to use Tkinter. If you've never used Tkinter before, I'll just say that it's one of the most frustrating Python modules I've used. It's very difficult to find any tutorials out there that are simple and code-light. After days of non-stop research, I finally pieced together the simplest way to create a Tkinter GUI. I will now relay the code to you.

The absolute simplest you can get for building a Tkinter GUI is the following code that produces the following GUI:

 from Tkinter import * 
 root = Tk() 
 mainloop() 

This GUI will stay open until you close the window. There's not that much to it, but it beats the nonsense of classes that other tutorials will immediately fling you into. So that you're not just thrown to the sharks, I'll explain the different elements of this code.

from Tkinter import * imports everything from the Tkinter module. I like to do this so that I don't end up with a strange error when I run my program that I eventually trace back to not having all the necessary modules imported. Be careful that you don't use tkinter or tk, since using either of those will thrown an error.

root = Tk() is where we define the GUI window. root  is what we call the window and Tk() tells Python that it needs to create a Tkinter window.

mainloop() keeps the GUI from closing itself immediately. Most tutorials I've found that show you how to make a simple Tkinter window will give you a few lines of code, but the GUI will only be open for a defined period of time before closing.

Next, I'll show you an example of a Tkinter GUI with GUI elements, like radio buttons, check boxes, text boxes, and labels.

 from Tkinter import * 
 root = Tk() 
 root.title("Title") 
 Label(root, text="Label").pack() 
 Entry(root).pack() 
 Radiobutton(root, text="Radiobutton").pack() 
 Checkbutton(root, text="Check box").pack() 
 Button(root, text="Button").pack() 
 mainloop() 


This is another bare bones Tkinter example. The input elements won't return values and are arranged based on how the elements are ordered in the code.

root.title("Title") sets the title of the GUI window. The program's code won't refer to the GUI by this name. You must associate the title with a specific window, so that is why root. is added.

.pack() is the simplest way to organize Tkinter GUI elements. The organization is based on the order of the lines of code. For instance, Label is above radio button in the above example code, so the label will appear above the radio button. .pack() isn't very customizable or controllable, but if just want to knock something together quickly, it works well.

Label(root, text="Label") created an uneditable line of text. The element is called Label because it is most frequently used to label another input element, such as a text entry box. The specific GUI window, root in this case, must be referenced so that Python knows what window it is putting the element in. This is the case for all the GUI elements. text= gives you the ability to label the element in the GUI.

Entry(root) makes a text entry box that you can use for data like a username, password, or journal entry. You can assign this element to a variable so your program can access the data entered by the user later in the program.

Radiobutton(root, text="Radiobutton") creates one radio button. If you want to make multiple radio buttons for a single choice, you need to associate them all with one variable. Adding , variable=newVar after text="Radiobutton" is the way to do that. Just be sure to define newVar.

Checkbutton(root, text="Check box") follows the same concepts as the preceding examples. It created a check box. For creating multiple check boxes for the same choice, you can use the same method as I described for radio buttons.

Button(root, text="Button") creates a button that does nothing. If you want it to do something, you create a function with no arguments, then reference that function. For instance, if you wanted to make the button quit the program:
 def dstryGUI(): 
     root.destroy() 
 ... 
 Button(root, text="Quit", command=dstryGUI) 

root.destroy() literally destroys the GUI window. After destroying the GUI window, if there isn't any further code, the program will quit.

Something to be careful of when creating a GUI: the mainloop() function runs the GUI window section of code for an indefinite period of time. That means, if you're trying to run other processes, the other processes won't run until you close the GUI. In fact, other processes, such as other GUI windows, will appear to hang and will become unresponsive. The best way to work around this is to create parallel processes, or multithreading. If you want to learn multithreading, I highly recommend reading this. It's part of an excellent book on Python that taught me most of what I know.

Comments

Popular Posts