Tags
In this post, we’ll have a brief introduction to GUI programming in Python, and will have a look at a couple of simple examples built using Python’s default GUI – Tkinter (Tk Interface).
Tk is a simple GUI toolkit that offers bindings in various languages and renders inalmost all platforms. Tkinter is based on Tk toolkit, originally designed for Tool Command Language (Tcl)
Hello World
Tkinter is builtin in Python. Go to Python Prompt and type in
>>> import tkinter
If it displays any error, maybe you need to recompile your Python interpreter after editing Modules/Setup file and enabling the settings for tkinter.
In our first example, we wil have a simple window that will display a Hellow World message in a Label. We will have a top-level element, called top, and add the Label widget to display the message. Plus we will add a Quit button, clicking which will terminate the application.
from tkinter import* top = Tk() helloLab = Label(text="Hello World!") helloLab.pack() quitBut = Button(text="Quit", command = top.quit) quitBut.pack() top.mainloop()
After importing tkinter modules, we created a top level window. We created a label called helloLab with the assigned text. Same way, a button called quitBut under it. We assigned a callback for our button to close the top level window when it is clicked. Widgets are packed to place them on the window. . Finally we called mainloop() to run the GUI.
A Simple Application (that actually does something)
We will build a small application that will accept two numbers from what we know as, TextBoxes, but tkinter calls as Entry Widgets. On pressing a Button, we will add the numbers and display the sum on a Label.
from tkinter import *
top = Tk()
resultStr= StringVar()
resultStr.set("Enter Numbers and Click the Button")
number1Label = Label (text="First Number")
number1Label.pack()
number1Box = Entry()
number1Box.pack()
number2Label = Label (text="Second Number")
number2Label.pack()
number2Box = Entry()
number2Box.pack()
resultLabel = Label(textvariable=resultStr)
resultLabel.pack()
def addNo():
no1 = int(number1Box.get())
no2 = int(number2Box.get())
no3 = no1+no2
resultStr.set( "The Sum is "+str(no3))
but = Button(text="Add", command=addNo)
but.pack()
top.mainloop()
Here, we first set a StringVar() type object in Python, that stores strings whose values can be modified and reflected on the widgets that display them. Labels display the message and Entry widgets take in entry from the user. We’ve tried to keep this program as simple as possible. Finally we have a button that is linked to the method addNo() which reads the numbers and updates the StringVar to display the result.
What Next?
This post was just an introduction to Python’s GUI capabilities using Tkinter. There are lots of widgets and Layout capabilities. We’ll be covering them in another blog post soon.
Along with Tkinter, there are many other GUI toolkits. wxPython and PyGTK are quite popular. You can have a look at the documentations and tutorials given in the references.
References
Python Wiki – GUI Programming displays a list of GUI toolkits compatible with python, along with links to their Wiki and documentations.
TkInter Wiki
A simple and straightforward tutorial form TutorialsPoint