0% found this document useful (0 votes)
57 views17 pages

Python GUI Development with Tkinter

Tkinter is the standard GUI library for Python that provides widgets for building graphical user interfaces. It includes common widgets like labels, buttons, text boxes, and more. Tkinter programs typically create a root window, add widgets to it, and enter the main event loop. For example, one can create a simple GUI with labels and buttons by importing Tkinter, creating a root window, adding labels and buttons to a frame, and calling mainloop(). Tkinter programs can also be organized into classes for improved structure.

Uploaded by

Dearly Wurtzbach
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views17 pages

Python GUI Development with Tkinter

Tkinter is the standard GUI library for Python that provides widgets for building graphical user interfaces. It includes common widgets like labels, buttons, text boxes, and more. Tkinter programs typically create a root window, add widgets to it, and enter the main event loop. For example, one can create a simple GUI with labels and buttons by importing Tkinter, creating a root window, adding labels and buttons to a frame, and calling mainloop(). Tkinter programs can also be organized into classes for improved structure.

Uploaded by

Dearly Wurtzbach
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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 = [Link]();
# the code will go here to add widgets...
[Link]();
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([Link]);

Below is the sample output, prints current tkinter version.

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:
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 = [Link]();
[Link]("CodesCracker Online Python Coding");
[Link]("400x400");

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);

Or

app = Frame(mainWin);

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

from tkinter import *;


mainWin = Tk();
[Link]("CodesCracker Labeler Program.");
[Link]("400x200");
myApp = Frame(mainWin);
[Link]();
lab = Label(myApp, text = "Hello!, this is a label.");
lab1 = Label(myApp, text = "python Class.");

[Link]();
[Link]();

[Link]();

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();
[Link]("Python GUI - Create Buttons");
[Link]("300x100");
app = Frame(win);
[Link]();

buttonOne = Button(app, text = "First Button");


[Link]();
buttonTwo = Button(app, text = "Second Button");
[Link]();
buttonThree = Button(app, text = "Third Button");
[Link]();

[Link]();

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();
[Link]("Python GUI - Creating Buttons in another way");
[Link]("300x100");
app = Frame(win);
[Link]();
buttonA = Button(app);
[Link]();
[Link](text = "Button A");
buttonB = Button(app);
[Link]();
buttonB["text"] = "Button B";
[Link]();

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);
[Link]();
self.create_widgets();

def create_widgets(self):
[Link] = Button(self, text = "This is first button");
[Link]();
[Link] = Button(self, text = "This is second button");
[Link]();
[Link] = Button(self, text = "This is third button");
[Link]();
myWin = Tk();
[Link]("Python GUI using class");
[Link]("300x100");
app = MyApp(myWin);
[Link]();

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);
[Link]();
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=[Link]);
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();
[Link]("Python GUI Password Protected Program");
[Link]("300x200");
app = MyApp(mainWindow);
[Link]();
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.

Common questions

Powered by AI

In Tkinter-based Python applications, Frames are container widgets that can hold and organize other widgets into blocks, making layout management more structured. Labels are used for displaying text or images and serve as non-interactive informational components, providing visual or textual context in the GUI .

Grid layout in a Python Tkinter GUI application allows you to divide the window into rows and columns, enabling precise placement of widgets. By setting grid attributes within widget definitions, you can arrange them in logical rows and columns, making the layout organized and easily adjustable .

The main event loop in Tkinter applications continuously listens for events like user inputs or widget interactions, ensuring the program responds appropriately. It facilitates dynamic interaction with the GUI by keeping the application responsive, allowing it to process events as they occur, and providing a stable runtime environment for the GUI .

Tkinter is highly usable for developing cross-platform GUI applications in Python due to its inclusion in the standard library, ensuring availability across many environments. Its simplicity and ease of use make it suitable for both beginners and professionals. However, its limitations include a less-modern appearance compared to other frameworks and less flexibility in design customization. Nevertheless, it remains a practical choice for straightforward applications .

In Python Tkinter, buttons can be created using the Button() function and configured with parameters like text to display. Configurations can be done using grid and direct assignments (e.g., button['text'] = 'Button B'). Using grid helps position buttons in the layout, and direct assignment provides flexibility for dynamic updates. Different methods offer various degrees of control over initialization and user interaction .

To create a basic GUI application in Python using Tkinter, follow these steps: first, import the Tkinter module; second, create the main window of the GUI application; third, add one or more widgets to the GUI application; finally, enter the main event loop to handle events triggered by user actions .

The 'root window' serves as the foundation of a GUI application in Python using Tkinter. It functions as the central node of the application, akin to the root of a tree. All other GUI elements or widgets are anchored to the root window either directly or indirectly, meaning all the branches and extensions of the application start from this primary window .

In a Tkinter application, the Entry widget is used for single-line text input, allowing users to enter small amounts of text like usernames or passwords. In contrast, the Text widget supports multiline text, making it suitable for larger text inputs or outputs, like paragraphs or formatted displays .

Using classes in structuring Python GUI code emphasizes organization and maintainability. It encapsulates the GUI logic and components into reusable objects, simplifying complex applications by segregating duties between different parts of the program, such as initialization and widget creation. This approach supports a modular structure, helps in avoiding redundancy, and makes the codebase more scalable and easier to debug .

A password protection mechanism can be implemented in Tkinter by using an Entry widget for password input and a Text widget for messages. A Button widget with a command function checks the entered password against a stored value. If correct, it alters the message to grant access; otherwise, it denies access. This setup uses event-driven programming to verify user credentials upon button activation .

You might also like