0% found this document useful (0 votes)
29 views

Tkinter GUI_NOTES1

Uploaded by

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

Tkinter GUI_NOTES1

Uploaded by

jincy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Introduction to GUI With Tkinter in Python

What Is A GUI?

Every time you interact with the screen on your computer or your phone you are more than liking using
a graphical user interface, or GUI (pronounced gooey). Those dazzling rectangular windows with buttons,
icons, and menus were created for us to have a much easier way to interact with our electronic devices. Older
methods, such as MS-DOS, used the command line and text.

A graphical user interface (GUI) is a desktop interface that allows you to communicate with computers.

What Is Tkinter?

Tkinter is Python's standard GUI package.

It is an object-oriented layer on top of the open-source Tcl/Tk widget toolkit.

While there are more feature complete packages available for creating a GUI, such as PyQt, Tkinter remains
popular as it is simple, widely used by beginners and has a ton of references to get you started

Tkinter is a standard Python GUI (Graphical User Interface) library that provides a set of tools and
widgets to create desktop applications with graphical interfaces.

Full Form of Tkinter

The name “Tkinter” comes from “Tk interface“, referring to the Tk GUI toolkit that Tkinter is based on.
Tkinter provides a way to create windows, buttons, labels, text boxes, and other GUI components to build
interactive applications.

Significance of Tkinter

Tkinter is the inbuilt python module that is used to create GUI applications. It is one of the most commonly
used modules for creating GUI applications in Python as it is simple and easy to work with. You don’t need
to worry about the installation of the Tkinter module separately as it comes with Python already. It gives an
object-oriented interface to the Tk GUI toolkit. Among all, Tkinter is most widely used.
Where is Python Tkinter used?

Here are some common use cases for Tkinter:

Creating windows and dialog boxes: Tkinter can be used to create windows and dialog boxes that allow
users to interact with your program. These can be used to display information, gather input, or present options
to the user.

Building a GUI for a desktop application: Tkinter can be used to create the interface for a desktop
application, including buttons, menus, and other interactive elements.

Adding a GUI to a command-line program: Tkinter can be used to add a GUI to a command-line program,
making it easier for users to interact with the program and input arguments.

Creating custom widgets: Tkinter includes a variety of built-in widgets, such as buttons, labels, and text
boxes, but it also allows you to create your own custom widgets.

In summary, Tkinter is a useful tool for creating a wide variety of graphical user interfaces, including windows,
dialog boxes, and custom widgets. It is particularly well-suited for building desktop applications and adding a
GUI to command-line programs.

Building Your First Python GUI Application With Tkinter

To create a Tkinter Python app, you follow these basic steps::

Import the tkinter module: This is done just like importing any other module in Python.

Create the main window (container): The main window serves as the container for all the GUI elements
you’ll add later.
Add widgets to the main window: You can add any number of widgets like buttons, labels, entry fields,
etc., to the main window to design the interface as desired.

Apply event triggers to the widgets: You can attach event triggers to the widgets to define how they respond
to user interactions

Create First Tkinter GUI Application

#STEP 1 – IMPORT TKINTER MODULES

import tkinter as tk

from tkinter import *

#STEP 2 CREATING A WINDOW

Let's start with the most basic idea: creating a window.


window = tk.Tk()

#STEP 3 – ADD TITLE AND WIDGETS

window.title(“my tkinter”)

Adding a Widget
Now that you have a window, you can add a widget.

Creating A Label
• Tkinter Label widget is used to display a text or image on the screen.
• To use a Label widget, you use the following general syntax:

Syntax

w = Label (window, options)


Parameters

window − This represents the parent window of the computer.

option − The list of most used options for this widget.

Option & Description

bd - The bd is used to change the Border width in pixels. Default is 2.

bg - The bg is used to change normal background color.

command - The command is used to call a function or method when the button is clicked

fg - The fg is used to change the normal foreground (text) color.

font - The font is used to change the text font to be used for the button's label.

Program

import tkinter as tk

from tkinter import *

window=tk.Tk()

label = Label(window, text = "MY


LABEL", bg = "green", bd = 100, fg = "white", font = "Castellar")

label.pack()

window.mainloop()
Button Widget
• The Button widget is used to add buttons in a Python application.
• These buttons can display text or images that convey the purpose of the buttons.
• You can attach a function or a method to a button which is called automatically when you click the
button.

Syntax

w = Button(window, options)

Parameters

• window − This represents the parent window.

• options − Here is the list of most commonly used options for this widget. These options can be used as

key-value pairs separated by commas.


PROGRAM

import tkinter as tk

from tkinter import *

window=tk.Tk()

btn = Button(window, text = 'Click me !')

# Set the position of button on the top of window

btn.pack(side = 'top')

window.mainloop()

Entry Widget

➢ The Entry widget is used to provde the single line text-box to the user to accept a value from the user.
➢ We can use the Entry widget to accept the text strings from the user.
➢ It can only be used for one line of text from the user.
➢ For multiple lines of text, we must use the text widget.

Syntax

w = Entry (window, options)

PROGRAM:

import tkinter as tk

from tkinter import *

window=tk.Tk()

L1 = Label(window, text="User Name")

L1.pack( side = LEFT)

E1 = Entry(window, bd =5)

E1.pack(side = RIGHT)

window.mainloop()
SAMPLE PROGRAMS
#1

Here is a simple program using tkinter that creates an entry widget, a button, and a label.
When the button is clicked, it will display the value from the entry widget in the label

PROGRAM:

import tkinter as tk

from tkinter import *

# Create the main window

window = tk.Tk()

window.title("Display Entry Widget Value")

# Function to display entry widget value in the label

def display_value():

value = entry.get()

label.config(text=f"Entered Value: {value}")

# Create an entry widget

entry = Entry(window, width=20)

entry.pack(pady=10)

# Create a button

btn = Button(window, text="Show Value", command=display_value)

btn.pack(pady=10)
# Create a label to display the entry value

label = Label(window, text="Entered Value: ")

label.pack(pady=10)

# Run the main loop

window.mainloop()
#2

Here's a program using tkinter that allows users to input two numbers, click a button to
add them, and then display the result in a label.

PROGRAM:

import tkinter as tk

from tkinter import *

# Create the main window

window = tk.Tk()

window.title("Add Two Numbers")

# Function to add the numbers and display the result

def add_numbers():

num1 = int(entry1.get())

num2 = int(entry2.get())

result = num1 + num2

label_result.config(text=f"Result: {result}")

# Create entry widgets for the two numbers

entry1 = Entry(window, width=10)

entry1.pack(pady=5)
entry2 = Entry(window, width=10)

entry2.pack(pady=5)

# Create a button to add the numbers

btn_add = Button(window, text="Add", command=add_numbers)

btn_add.pack(pady=10)

# Create a label to display the result

label_result = Label(window, text="Result: ")

label_result.pack(pady=10)

# Run the main loop

window.mainloop()
#3

Here's a tkinter program that includes entry widgets for a username and password, a button

to submit them, and a label to display the entered username and password.

PROGRAM:

import tkinter as tk

from tkinter import *

# Create the main window

window = tk.Tk()

window.title("Username and Password")

# Function to display entered username and password

def display_credentials():

username = entry_username.get()

password = entry_password.get()

label_result.config(text=f"Username: {username}\nPassword: {password}")

# Create entry widgets for username and password

label_username = Label(window, text="Username:")

label_username.pack(pady=5)

entry_username = Entry(window, width=20)

entry_username.pack(pady=5)
label_password = Label(window, text="Password:")

label_password.pack(pady=5)

entry_password = Entry(window, show='*', width=20)

entry_password.pack(pady=5)

# Create a button to submit the credentials

btn_submit = Button(window, text="Submit", command=display_credentials)

btn_submit.pack(pady=10)

# Create a label to display the entered credentials

label_result = Label(window, text="Entered Credentials:")

label_result.pack(pady=10)

# Run the main loop

window.mainloop()
MessageBox Widget

Python Tkinter – MessageBox Widget is used to display the message boxes in the python applications.
This module is used to display a message using provides a number of functions.

There are functions or methods available in the messagebox widget


.
1. showinfo(): Show some relevant information to the user.

2. showwarning(): Display the warning to the user.

3. showerror(): Display the error message to the user.

4. askquestion(): Ask question and user has to answered in yes or no.

5. askokcancel(): Confirm the user’s action regarding some application activity.

6. askyesno(): User can answer in yes or no for some action.

7. askretrycancel(): Ask the user about doing a particular task again or not.

.
PROGRAM

Import tkinter as tk

from tkinter import *

from tkinter import messagebox

window = Tk()

messagebox.showinfo("showinfo", "Information")

messagebox.showwarning("showwarning", "Warning")

messagebox.showerror("showerror", "Error")

messagebox.askquestion("askquestion", "Are you sure?")

messagebox.askokcancel("askokcancel", "Want to continue?")

messagebox.askyesno("askyesno", "Find the value?")

messagebox.askretrycancel("askretrycancel", "Try again?")

window.mainloop()

You might also like