Python GUI Programming

GUI stands for Graphical User Interface, python provides you, various options for developing the GUIs.

Tkinter Programming

Tkinter is the standard GUI library available for python. Python when combined with Tkinter, provides a fast and easy way to create a GUI applications.

Tkinter provides a very powerful object-oriented interface to the Tk GUI toolkit.

Here are the steps listed, to keep in mind for creating any GUI applications in python:

  • first import the Tkinter module
  • now create the GUI application main window
  • add one/more widgets to the GUI application
  • now enter the main event loop to take an action against each event that is triggered by the user

Python GUI Programming Example

Here is an example of gui programming using python.

# Python GUI Programming - Example Program 

import tkinter;
top = tkinter.Tk();
# the code will go here to add widgets...

It will create a window with title tk as shown in the sample screenshot given below.

python gui programming

Now let's go through Python gui programming little deeply.

Python Print tkinter Version

Here is an example used to print the tkinter version of python.

import tkinter;

Below is the sample output, prints current tkinter version.

python gui tkinter

Root Window

Basically, the foundation of a GUI python program is its root window, upon which you can add all the other GUI elements.

In simple, if GUI is a tree, then root window is its root. It means, the tree (GUI) can branch out in all directions, but every of its part are anchored by the root (root window) either directly or indirectly.

Importing tkinter Module

Here is the code shows how to import tkinter module.

from tkinter import *;

Above python code fragment import all the functions of tkinter module.

Creating a Root Window

To create a root window in python GUI (Graphical User Interface) programming or development, follow the code given below:

root = Tk();

Python Simplest GUI Program

Here is an example shows a simplest and first GUI windows created using python.

from tkinter import *;
root = Tk();

If you will run or execute the above GUI code of python, then here is the output you will see on your screen:

python gui create root window

Modifing a Root Window

Now let's modify a root window as created earlier with some of the given code fragment of Python GUI programming.

import tkinter;
mainWin = tkinter.Tk();
mainWin.title("CodesCracker Online Python Coding");

Here is the sample output produced by the above example.

python gui modify root window

Python GUI Labels

GUI (Graphical User Interface) elements are widgets or windows gadgets. The simplest windows gadget is the Label windows gadget which is non-editable icons or text or both.

Create a Frame

In Python GUI, a frame is simply a widget (windows gadget) that can hold other widgets.

Below code fragment is used to create frame.

myApp = Frame(root);


app = Frame(mainWin);

Create a Label

Let's create a label in Python GUI using the following code.

from tkinter import *;
mainWin = Tk();
mainWin.title("CodesCracker Labeler Program.");
myApp = Frame(mainWin);
lab = Label(myApp, text = "Hello!, this is a label.");

Here is the sample output of the above example.

python gui create label

Python GUI Buttons

In Python GUI, buttons are the widgets that can be activated by the user just to perform some action.

Python GUI Create Buttons

You can use Button() function to create buttons in Python GUI.

Here is an example shows how to create buttons in python GUI.

from tkinter import *;
win = Tk();
win.title("Python GUI - Create Buttons");
app = Frame(win);
buttonOne = Button(app, text = "First Button");
buttonTwo = Button(app, text = "Second Button");
buttonThree = Button(app, text = "Third Button");

Here is the sample output produced by the above creating buttons using python example code.

python gui create buttons

Let's take another example of creating buttons in python gui with another way as shown below.

from tkinter import *;
win = Tk();
win.title("Python GUI - Creating Buttons in another way");
app = Frame(win);
buttonA = Button(app);
buttonA.configure(text = "Button A");
buttonB = Button(app);
buttonB["text"] = "Button B";

Below is the screenshot of the sample output produced by above example.

python gui buttons

Python GUI Development using Class

As you already knows that organizing your python code into classes makes your programming task more easier.

Here is an example of GUI development using python class.

Let's create three buttons using class in python.

from tkinter import *;
class MyApp(Frame):
    def __init__(self, master):
        Frame.__init__(self, master);
    def create_widgets(self):
        self.myFirstButton = Button(self, text = "This is first button");
        self.mySecondButton = Button(self, text = "This is second button");
        self.myThirdButton = Button(self, text = "This is third button");
myWin = Tk();
myWin.title("Python GUI using class");
app = MyApp(myWin);

Here is the sample output produced by the above example code of creating python gui using class.

python gui using class

Text and Entry Widgets

In python, the entry widget is good only for one line of text, whereas the text widget is suitable or perfect for multiline blocks of text.

Python GUI using Grid Layout Manager

Python development using grid layout manager means dividing the windows into row and columns. Here is the grid layout row and column with its values for general understanding before going through program.

python gui grid layout manager

Python GUI Password Protected Program

This gui password protected program using python, contains event handling code, that you will learn about it in next tutorial.

In this example, user ask to enter their password to grant access. If he/she will enter the correct password (here codescracker), then he/she is the authorised person, or if he/she will enter the wrong password, then he/she isn't authorised person.

Let's go through the following password protected program in python GUI.

from tkinter import *;
class MyApp(Frame):
    def __init__(self, master):
        Frame.__init__(self, master);
    def create_widget(self):
        self.instruction_label = Label(self, text = "Only authorised person are allowed.");
        self.instruction_label.grid(row=0, column=0, columnspan=2, sticky=W);
        self.password_label = Label(self, text = "Enter Password");
        self.password_label.grid(row=1, column=0, sticky=W);
        self.password_entry = Entry(self);
        self.password_entry.grid(row=1, column=1, sticky=W);
        self.submit_button = Button(self, text = "Login", command=self.reveal);
        self.submit_button.grid(row=2, column=0, sticky=W);
        self.secret_text = Text(self, width=35, height=5, wrap=WORD);
        self.secret_text.grid(row=3, column=0, columnspan=2, sticky=W);
    def reveal(self):
        contents = self.password_entry.get();
        if contents == "codescracker":
            message = "Congrats!\nYou are the authorised person.\nWelcome sir.";
            message = "Wrong password!\nYou are not authorised to access.";
        self.secret_text.delete(0.0, END);
        self.secret_text.insert(0.0, message);
mainWindow = Tk();
mainWindow.title("Python GUI Password Protected Program");
app = MyApp(mainWindow);

Here is the sample initial output produced by above password protected example.

python text entry widgets

Now let's type any password say forgotten as shown in the following screenshot.

python grid layout manager

Now press the Login button to watch the output as shown below.

python gui password protected program

Now type the correct password, that is codescracker and press login to see the following output.

python placing widget with grid

Python Online Test

« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook

Subscribe Us on YouTube