0% found this document useful (0 votes)
129 views4 pages

Tkinter Spinbox and GUI Basics

Unit 6 covers Python GUI programming using Tkinter, the standard library for creating graphical user interfaces. It outlines the basic steps to create a Tkinter application, including importing the module, creating a main window, adding widgets, and applying event triggers. The document also describes various Tkinter widgets and geometry management methods for organizing them within the application.

Uploaded by

tkulkarni2007
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)
129 views4 pages

Tkinter Spinbox and GUI Basics

Unit 6 covers Python GUI programming using Tkinter, the standard library for creating graphical user interfaces. It outlines the basic steps to create a Tkinter application, including importing the module, creating a main window, adding widgets, and applying event triggers. The document also describes various Tkinter widgets and geometry management methods for organizing them within the application.

Uploaded by

tkulkarni2007
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

Unit 6

1. Python - GUI Programming using Tkinter: Python provides various options for
developing graphical user interfaces (GUIs). Tkinter is the standard GUI library for
Python. Python when combined with Tkinter provides a fast and easy way to create GUI
applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit.
 GUI refers to graphical user interfaces, and Tkinter is Python’s built-in library for
creating them.
 Tkinter is included with most Python installations, so separate installation is often
unnecessary.
 Tkinter is still a relevant choice for building simple, cross-platform GUI
applications.
 Widgets in a Tkinter application can be arranged using geometry managers like
.pack(), .place(), and .grid().
 Interactive GUI applications with Tkinter are created by binding events, such as
button clicks, to Python functions.

i. To create a Tkinter Python application, you follow these basic steps:

1. Import the tkinter module: This is done just like importing any other module in
Python.
2. Create the main window (container): The main window serves as the container
for all the GUI elements you’ll add later.
3. 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.
4. Apply event triggers to the widgets: Enter the main event loop to take action
against each event triggered by the user. You can attach event triggers to the widgets
to define how they respond to user interactions.

ii. Create First Tkinter GUI Application:


The foundational element of a Tkinter GUI is the window. Windows are the
containers in which all other GUI elements live. These other GUI elements, such as
text boxes, labels, and buttons, are known as widgets. Widgets are contained inside of
windows.
first thing you need to do is import the Python GUI Tkinter module:
import tkinter as tk
A window is an instance of Tkinter’s Tk class. Go ahead and create a new window
and assign it to the variable window:
window = [Link]()
mainloop(): There is a method known by the name mainloop() is used when your
application is ready to run. mainloop() is an infinite loop used to run the application,
wait for an event to occur, and process the event as long as the window is not closed.
Example 1- (Practical no 24-a)
import tkinter as tk
# Create instance
parent = [Link]()
# Add a title
[Link]("-Welcome to Python tkinter Basic exercises-")
# Start GUI
[Link]()

iii. Tkinter Widgets: There are a number of widgets(GUI elements) which you
can put in your tkinter application. Some of the major widgets are explained
below-
Button
1
The Button widget is used to display the buttons in your application.

Canvas
2 The Canvas widget is used to draw shapes, such as lines, ovals, polygons and
rectangles, in your application.

Checkbutton
3 The Checkbutton widget is used to display a number of options as checkboxes. The
user can select multiple options at a time.

Entry
4 The Entry widget is used to display a single-line text field for accepting values from a
user.

Frame
5
The Frame widget is used as a container widget to organize other widgets.

Label
6 The Label widget is used to provide a single-line caption for other widgets. It can also
contain images.

Listbox
7
The Listbox widget is used to provide a list of options to a user.

Menubutton
8
The Menubutton widget is used to display menus in your application.

Menu
9 The Menu widget is used to provide various commands to a user. These commands
are contained inside Menubutton.

10 Message
The Message widget is used to display multiline text fields for accepting values from a
user.

Radiobutton
11 The Radiobutton widget is used to display a number of options as radio buttons. The
user can select only one option at a time.

Scale
12
The Scale widget is used to provide a slider widget.

Scrollbar
13 The Scrollbar widget is used to add scrolling capability to various widgets, such as list
boxes.

Text
14
The Text widget is used to display text in multiple lines.

Toplevel
15
The Toplevel widget is used to provide a separate window container.

Spinbox
16 The Spinbox widget is a variant of the standard Tkinter Entry widget, which can be
used to select from a fixed number of values.

PanedWindow
17 A PanedWindow is a container widget that may contain any number of panes,
arranged horizontally or vertically.

LabelFrame
18 A labelframe is a simple container widget. Its primary purpose is to act as a spacer or
container for complex window layouts.

tkMessageBox
19
This module is used to display message boxes in your applications.

iv. Geometry Management: All Tkinter widgets have access to the specific
geometry management methods, which have the purpose of organizing
widgets throughout the parent widget area. Tkinter exposes the following
geometry manager classes: pack, grid, and place.
 The pack() Method − This geometry manager organizes widgets in
blocks before placing them in the parent widget.
 The grid() Method − This geometry manager organizes widgets in a
table-like structure in the parent widget.
 The place() Method − This geometry manager organizes widgets by
placing them in a specific position in the parent widget.
v. Resources:
i. [Link]
ii. [Link]
iii. [Link]

Example 2- (Practical no 24-b) button example


from tkinter import *
parent = Tk()
redbutton = Button(parent, text = "Red", fg = "red")
[Link]( side = LEFT)
greenbutton = Button(parent, text = "Black", fg = "black")
[Link]( side = RIGHT )
bluebutton = Button(parent, text = "Blue", fg = "blue")
[Link]( side = TOP )
blackbutton = Button(parent, text = "Green", fg = "red")
[Link]( side = BOTTOM)
[Link]()

Example 3- (Practical no 24-b) label-button-entry example


from tkinter import *
parent = Tk()
name = Label(parent,text = "Name").grid(row = 0, column = 0)
e1 = Entry(parent).grid(row = 0, column = 1)
password = Label(parent,text = "Password").grid(row = 1, column = 0)
e2 = Entry(parent).grid(row = 1, column = 1)
submit = Button(parent, text = "Submit").grid(row = 4, column = 0)
[Link]()

You might also like