Tkinter GUI_NOTES1
Tkinter GUI_NOTES1
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?
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.
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?
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.
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
import tkinter as tk
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
command - The command is used to call a function or method when the button is clicked
font - The font is used to change the text font to be used for the button's label.
Program
import tkinter as tk
window=tk.Tk()
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
• options − Here is the list of most commonly used options for this widget. These options can be used as
import tkinter as tk
window=tk.Tk()
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
PROGRAM:
import tkinter as tk
window=tk.Tk()
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
window = tk.Tk()
def display_value():
value = entry.get()
entry.pack(pady=10)
# Create a button
btn.pack(pady=10)
# Create a label to display the entry value
label.pack(pady=10)
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
window = tk.Tk()
def add_numbers():
num1 = int(entry1.get())
num2 = int(entry2.get())
label_result.config(text=f"Result: {result}")
entry1.pack(pady=5)
entry2 = Entry(window, width=10)
entry2.pack(pady=5)
btn_add.pack(pady=10)
label_result.pack(pady=10)
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
window = tk.Tk()
def display_credentials():
username = entry_username.get()
password = entry_password.get()
label_username.pack(pady=5)
entry_username.pack(pady=5)
label_password = Label(window, text="Password:")
label_password.pack(pady=5)
entry_password.pack(pady=5)
btn_submit.pack(pady=10)
label_result.pack(pady=10)
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.
7. askretrycancel(): Ask the user about doing a particular task again or not.
.
PROGRAM
Import tkinter as tk
window = Tk()
messagebox.showinfo("showinfo", "Information")
messagebox.showwarning("showwarning", "Warning")
messagebox.showerror("showerror", "Error")
window.mainloop()