Python GUI Programming
GUI stands for Graphical User Interface, python provides you, various options for developing the GUIs.
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... top.mainloop();
It will create a window with title tk as shown in the sample screenshot given below.
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; print(tkinter.TkVersion);
Below is the sample output, prints current tkinter version.
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:
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"); mainWin.geometry("400x200");
Here is the sample output produced by the above example.
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."); mainWin.geometry("400x200"); myApp = Frame(mainWin); myApp.grid(); lab = Label(myApp, text = "Hello!, this is a label."); lab.grid(); mainWin.mainloop();
Here is the sample output of the above example.
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"); win.geometry("300x100"); app = Frame(win); app.grid(); buttonOne = Button(app, text = "First Button"); buttonOne.grid(); buttonTwo = Button(app, text = "Second Button"); buttonTwo.grid(); buttonThree = Button(app, text = "Third Button"); buttonThree.grid(); win.mainloop();
Here is the sample output produced by the above creating buttons using python example code.
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"); win.geometry("300x100"); app = Frame(win); app.grid(); buttonA = Button(app); buttonA.grid(); buttonA.configure(text = "Button A"); buttonB = Button(app); buttonB.grid(); buttonB["text"] = "Button B"; win.mainloop();
Below is the screenshot of the sample output produced by above example.
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); self.grid(); self.create_widgets(); def create_widgets(self): self.myFirstButton = Button(self, text = "This is first button"); self.myFirstButton.grid(); self.mySecondButton = Button(self, text = "This is second button"); self.mySecondButton.grid(); self.myThirdButton = Button(self, text = "This is third button"); self.myThirdButton.grid(); myWin = Tk(); myWin.title("Python GUI using class"); myWin.geometry("300x100"); app = MyApp(myWin); myWin.mainloop();
Here is the sample output produced by the above example code of creating 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 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); self.grid(); self.create_widget(); 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."; else: 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"); mainWindow.geometry("300x200"); app = MyApp(mainWindow); mainWindow.mainloop();
Here is the sample initial output produced by above password protected example.
Now let's type any password say forgotten as shown in the following screenshot.
Now press the Login button to watch the output as shown below.
Now type the correct password, that is codescracker and press login to see the following output.
« Previous Tutorial Next Tutorial »
Follow/Like Us on Facebook
Subscribe Us on YouTube