Graphics Library in Python: Definition & Examples

Instructor: Renata Mcfadden

Dr McFadden has 20+ years of experience in IT and over 12 years in teaching college courses across multiple colleges, both in person and online.

In this lesson, you will learn how the Python Tkinter graphics library works and how to create a simple graphical user interface (GUI) in Python using a few basic widgets.

Graphics Library

Tkinter is one of Python's standard and easy-to-use Graphical User Interface (GUI) libraries that normally comes bundled with Python. There are many others with additional capabilities, platform support, and additional modern widgets.

In general, when writing GUI code you need to consider the platform your code will be running on, the widgets you will need, and whether the library needs bindings for other languages such as C or C++ or other toolkits.

Tkinter is platform independent and has all the standard widgets you may want to use such as labels, text boxes, buttons, menus, etc. And once you learn the basics of coding a GUI, you can apply those skills to the use of any other graphics library.

Graphics Window

To get started we will write the code to display our graphics window with a title ''Simple GUI.'' To do so, we will import the tkinter library and create an instance of the Tk GUI toolkit:

#!/usr/bin/python3
from tkinter import *
class SimpleGUI:
    def __init__(self, master):
        self.master = master
        master.title(''Simple GUI'')
# main
master = Tk() # creates an instance of GUI
SimpleGUI(master)
master.mainloop() # makes the graphics window stay up

In this code we create an instance of Tk and pass the reference master to our constructor so we can add and change the content of the GUI from any of our methods. We also add a title ''Simple GUI.''

When you run this code, you will get a pop-up graphics screen. Notice it has the title we specified.

Now we are ready to add some widgets.

Widgets

Widgets are software components that have a certain look and behavior. For example, a label is a box with some text in it and it normally cannot be modified by a user. A text box, however, may allow a user to input some data that may be used by the program to do something.

When designing and coding a GUI, we use the library's existing widgets to save time coding and so that the look and feel of the widget is as expected by the users.

We will now create a GUI that allows the user to enter a name, year of birth, and it will display the age.

Add Label

To add a widget to the GUI, we create a widget and we designate where to place it. For example:

self.labelName = Label(master, text=''Name'')
self.labelName.grid(row=1, column=1)

In this code we create a Label widget called labelName with text ''Name'' and we place it in the top left corner of our GUI using the grid method. The top left corner has location (row=0, column=0). If you add these two lines to the above constructor and run it, your GUI will look like the image below.

Image1 Label

Add Entry

Next, we want to add a field where the user can enter his/her name. To do so, we create an Entry widget and place it on the same row next to the label:

self.name = Entry(master)
self.name.grid(row=1, column=5)

Image 2 Entry widget

Add Additional Label and Entry Widgets

Next, let's add a Label for the year and an Entry next to it, below the name widgets, as well as a Label for the age:

self.labelYear = Label(master, text=''Year'')
self.labelYear.grid(row=10, column=1)
self.year = Entry(master)
self.year.grid(row=10, column=5)
self.labelAge = Label(master, text=''Age = '')
self.labelAge.grid(row=20, column=1)

Image 3 widgets

Add Button

Next we want to add two Button widgets—one button the user can click to calculate the age and another one to exit the GUI.

The button for exiting will have text 'EXIT' on it and we specify that it should quit the GUI by giving it the command master.quit. Here is the code:

self.exit = Button(master, text=''EXIT'', command=master.quit)
self.exit.grid(row=30, column=2)

The button for calculating age will call our method calc:

self.calculate = Button(master, text=''Calculate'', command=self.calc)
self.calculate.grid(row=30, column=1)

To run all the code you need to create the calc function with no code:

def calc(self):
    pass

Our GUI now has all the widgets we need:

Image 4 Buttons

Add Event Handling

The last step is to add code to the calc function to calculate the age and display it next to the age label.

To determine the current year, we can use the date from the datetime library. Then we get the value from the year Entry and convert it to the integer. Lastly, we subtract the user-provided year from the current year and display the value in a label.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support