0% found this document useful (0 votes)
7 views65 pages

Python Gui Programming- Tkinter

The document outlines a 24-hour workshop on project development using Python, focusing on GUI programming with Tkinter. It covers various Tkinter widgets, their attributes, and how to create and manage them, including buttons, checkbuttons, frames, and more. Additionally, it provides syntax examples and code snippets for practical implementation of these widgets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views65 pages

Python Gui Programming- Tkinter

The document outlines a 24-hour workshop on project development using Python, focusing on GUI programming with Tkinter. It covers various Tkinter widgets, their attributes, and how to create and manage them, including buttons, checkbuttons, frames, and more. Additionally, it provides syntax examples and code snippets for practical implementation of these widgets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 65

24 HOUR HACKATHON –

PROJECT DEVELOPMENT
USING PYTHON
MOUNT ZION COLLEGE OF ENGINEERING AND TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

24 HOUR WORKSHOP- PROJECT DEVELOPMENT USING PYTHON

SATHYA. M, AP/CSE
24 HOURS WORKSHOP ON
PYTHON GUI PROGRAMMING WITH TKINTER
TKINTER INTRODUCTION

• Python provides the standard library Tkinter for creating the graphical user interface for
desktop based applications.

• Tkinter is the most commonly used library for developing GUI (Graphical User Interface) in
Python.

• It is a standard Python interface to the Tk GUI toolkit shipped with Python.


An empty Tkinter top-level window can be created by using the following steps.

1.import the Tkinter module.

2.Create the main application window.

3.Add the widgets like labels, buttons, frames, etc. to the window.

4.Call the main event loop so that the actions can take place on the user's computer screen.
Import module
Tkinter is the standard library.This can be imported as module in our project to use the
functionalities provides by the library for creating GUI in python and python based frameworks.

Syntax:

from tkinter import *


Button

Creation of Button using tk themed widget (tkinter.ttk). This will give you the effects of modern
graphics. Effects will change from one OS to another because it is basically for the appearance.
Syntax: w = Button ( master, options)

# import everything from tkinter module

from tkinter import *

# create a tkinter window

root = Tk()

# Open window having dimension 100x100

root.geometry('100x100')

# Create a Button

btn = Button(root, text = 'Click me !', bd = '5',command = root.destroy)

# Set the position of button on the top of window.


output
STANDARD ATTRIBUTES

In tkinter, there are several standard attributes commonly used when


working with GUI elements like text,font,bg etc... These attributes allow
you to customize the appearance and behavior of widgets . The standard widget
attributes are keywords used in widget constructors.

Tkinter widget state-It can have the following values: NORMAL, ACTIVE, and DISABLED.
Tkinter widget padding -The padx and pady attributes add extra horizontal and
vertical space to the widgets.
Tkinter Background colours It can be abbreviated to bg.
Likewise, the foreground colours of widgets can be set with foreground attribute. It can
be abbreviated to fg.
Width and height The width and height attributes set the width and height of the
widget.
Tkinter reliefs A relief is a border decoration. The possible values
are: SUNKEN, RAISED, GROOVE, RIDGE, and FLAT.
Tkinter fonts Tkinter has a tkinter.font module for working with fonts. It has some
built-in fonts such as TkTooltipFont, TkDefaultFont, or TkTextFont. The font is set
with the font attribute.
Tkinter cursors
The cursor is a small icon that shows where the mouse pointer is located. The cursor in
Tkinter is set with the cursor attribute.
Geometry management

Geometry management in tkinter refers to the process of organizing and


positioning widgets within a window or container. Tkinter provides
three main geometry managers to achieve this:

• pack(),

• grid()

• place().
The Pack geometry manager packs widgets relative to the earlier widget. Tkinter literally packs all
the widgets one after the other in a window.
# Importing tkinter module
from tkinter import * from tkinter.ttk import *
# creating Tk window
master = Tk()
# creating a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)

# button widgets which can also expand and fill


# in the parent widget entirely
# Button 1

b1 = Button(pane, text = "Click me !")

b1.pack(fill = BOTH, expand = True)

# Button 2

b2 = Button(pane, text = "Click me too")

b2.pack(fill = BOTH, expand = True)

# Execute Tkinter

master.mainloop()
OUTPUT
from tkinter import *

master = Tk()

# creating a Fra, e which can expand according to the size of the window

pane = Frame(master)

pane.pack(fill = BOTH, expand = True)

# button widgets which can also expand and fill

b1 = Button(pane, text = "Click me !",background = "red", fg = "white")

b1.pack(side = TOP, expand = True, fill = BOTH)

# Button 2

b2 = Button(pane, text = "Click me too",background = "blue", fg = "white")

b2.pack(side = TOP, expand = True, fill = BOTH)

# Button 3

b3 = Button(pane, text = "I'm also button",background = "green", fg = "white")

b3.pack(side = TOP, expand = True, fill = BOTH)

master.mainloop()
Canvas
The Canvas widget is used to draw shapes, such as lines, ovals, polygons and rectangles, in your
application. B. It serves as a blank canvas where you can create and manipulate graphical elements
dynamically.

Syntax:

C = Canvas(root, height, width, bd, bg, ..)


Canvas:tcanvas.py

from tkinter import *

root=Tk()

C= Canvas(root, height=500,width=500,bg="green")

C.pack()

line=C.create_line(0,0,600,500,fill="red",dash=(3,3),width=5)

rec=C.create_rectangle(150,125,450,375,fill="blue",width=5)

root.mainloop()
Output
Canvas1:tcanvas1.py

from tkinter import *

root = Tk()

C = Canvas(root, bg="yellow",height=400, width=400)

line = C.create_line(108, 120,320, 40,fill="green")

arc = C.create_arc(180, 150, 80,210, start=0,extent=220,fill="red")

oval = C.create_oval(80, 30, 140,150,fill="blue")

C.pack()

mainloop()
output
CheckButton

The Checkbutton widget is used to present a set of options or choices to the user. It allows
the user to select one or multiple options by clicking on the checkboxes associated with
each option.

SYNTAX:

• checkbutton1 = tk.Checkbutton(root, text="Option 1", variable=option1_var)

• checkbutton2 = tk.Checkbutton(root, text="Option 2", variable=option2_var)


CheckButton:tcheckbut.py

from tkinter import *

root = Tk()

root.geometry("300x200")

w = Label(root, text =‘courses offered', font = "50")

w.pack()

Checkbutton1 = IntVar()

Checkbutton2 = IntVar()

Checkbutton3 = IntVar()

Button1 = Checkbutton(root, text = “python",variable = Checkbutton1,onvalue = 1,offvalue


Button1.pack()

Button2.pack()

Button3.pack()

mainloop()
output
CheckButton:tcheckbut1.py

from tkinter import *

import tkinter.messagebox as messagebox

root=Tk()

root.geometry("400x400")

ck1=IntVar()

ck2=IntVar()

ck3=IntVar()

def check():

if ck1.get() == 1:

messagebox.showinfo("Hello","You pressed C")

elif ck2.get()==1:

messagebox.showinfo("Hello","You pressed python")


CheckButton:tcheckbut1.py

elif ck3.get()==1:

messagebox.showinfo("Hello","You pressed Java")

else:

pass

chkbut1=Checkbutton(root,text="C",variable=ck1,onvalue=1,offvalue=0,height=2,width=10,bg="
red",fg="blue")

chkbut2=Checkbutton(root,text="python",
variable=ck2,onvalue=1,offvalue=0,height=2,width=10)

chkbut3=Checkbutton(root,text="Java", variable=ck3,onvalue=1,offvalue=0,height=2,width=10)

but=Button(root,text="ClicKHere", width=10,fg="yellow",command=check)
CheckButton:tcheckbut1.py

chkbut1.pack()

chkbut2.pack()

chkbut3.pack()

but.pack()

root.mainloop()
output
Entry

• The Entry Widget is a Tkinter Widget used to Enter or display a single line of text.

• It is commonly used in forms and other where user input is required.

SYNTAX:
entry = tk.Entry(parent, options)
import tkinter as tk
root=tk.Tk()
root.geometry("600x400")
name_var=tk.StringVar()
passw_var=tk.StringVar()
def submit():
name=name_var.get()
password=passw_var.get()
print("The name is : " + name)
print("The password is : " + password)
name_var.set("")
passw_var.set("")
name_label = tk.Label(root, text = 'Username', font=('calibre',10, 'bold'))
name_entry = tk.Entry(root,textvariable = name_var, font=('calibre',10,'normal'))
passw_label = tk.Label(root, text = 'Password', font = ('calibre',10,'bold'))
passw_entry=tk.Entry(root, textvariable = passw_var, font = ('calibre',10,'normal'),
show = '*')
sub_btn=tk.Button(root,text = 'Submit', command = submit)
name_label.grid(row=0,column=0)
name_entry.grid(row=0,column=1)
passw_label.grid(row=1,column=0)
passw_entry.grid(row=1,column=1)
sub_btn.grid(row=2,column=1)
root.mainloop()
Frame

A Frame is a rectangular container that serves as a grouping or organizational widget. It


provides a way to organize and manage other widgets within a window or another frame.

SYNTAX:

frame = tk.Frame(root)
from tkinter import *
root = Tk()
root.geometry("300x150")
w = Label(root, text ='cse', font = "50")
w.pack()
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
b1_button = Button(frame, text ="AI", fg ="red")
b1_button.pack( side = LEFT)
b2_button = Button(frame, text ="CLOUD", fg ="brown")
b2_button.pack( side = LEFT )
b3_button = Button(frame, text ="DS", fg ="blue")
b3_button.pack( side = LEFT )
b4_button = Button(bottomframe, text ="ML", fg ="green")
b4_button.pack( side = BOTTOM)
b5_button = Button(bottomframe, text ="OS", fg ="green")
b5_button.pack( side = BOTTOM)
b6_button = Button(bottomframe, text ="TOC", fg ="green")
b6_button.pack( side = BOTTOM)
root.mainloop()
OUTPUT
Listbox

The Listbox widget provides a way to display a list of items from which the user can select
one or more items.The Listbox widget will display the inserted items, and the user can
select one or more items from the list.

SYNTAX:

listbox = tk.Listbox(root)
from tkinter import *
top = Tk()
listbox = Listbox(top, height = 10,width = 15,bg = "grey",activestyle = 'dotbox',font
= "Helvetica",
fg = "yellow")
top.geometry("300x250")
label = Label(top, text = " FOOD ITEMS")
listbox.insert(1, "Nachos")
listbox.insert(2, "Sandwich")
listbox.insert(3, "Burger")
listbox.insert(4, "Pizza")
listbox.insert(5, "Burrito")
label.pack()
listbox.pack()
top.mainloop()
OUTPUT
Label

The Label widget is used to display text or images on the screen. It is a simple widget that
provides a way to show static content within a tkinter window or frame.

SYNTAX:

label = tk.Label(root, text="Hello, World!")


from tkinter import *
top = Tk()
top.geometry("450x300")
user_name = Label(top,text = "Username").place(x = 40,y = 60)
user_password = Label(top,text = "Password").place(x = 40,y = 100)
submit_button = Button(top,text = "Submit").place(x = 40,y = 130)
user_name_input_area = Entry(top,width = 30).place(x = 110,y = 60)
user_password_entry_area = Entry(top,width = 30).place(x = 110,y = 100)
top.mainloop()
OUTPUT
Menu

The Menu widget is used to create menus in a graphical user interface

(GUI). Menus provide a way to organize and present a list of options or

commands to the user.

SYNTAX:

menu = tk.Menu(menubutton)
from tkinter import *
from tkinter.ttk import *
from time import strftime
root = Tk()
root.title('Menu Demonstration')
# Creating Menubar
menubar = Menu(root)
# Adding File Menu and commands
file = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='File', menu = file)
file.add_command(label ='New File', command = None)
file.add_command(label ='Open...', command = None)
file.add_command(label ='Save', command = None)
file.add_separator()
file.add_command(label ='Exit', command = root.destroy)
# Adding Edit Menu and commands
menubar.add_cascade(label ='Edit', menu = edit)
edit.add_command(label ='Cut', command = None)
edit.add_command(label ='Copy', command = None)
edit.add_command(label ='Paste', command = None)
edit.add_command(label ='Select All', command = None)
edit.add_separator()
edit.add_command(label ='Find...', command = None)
edit.add_command(label ='Find again', command = None)
# Adding Help Menu
help_ = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Help', menu = help_)
help_.add_command(label ='Tk Help', command = None)
help_.add_command(label ='Demo', command = None)
help_.add_separator()
help_.add_command(label ='About Tk', command = None)
root.config(menu = menubar)
Menubutton

The Menubutton widget is used to create a drop-down menu with selectable options. It
provides a way to present a set of choices or actions to the user in a hierarchical menu
structure.

SYNTAX:

menubutton = tk.Menubutton(root, text="Options")


Radiobutton

A Radio button is a widget used to present a set of mutually exclusive options to the user. It
allows the user to select only one option from a group of options.

SYNTAX:

radio_button1 = tk.Radiobutton(root, text="Option 1", variable=selected_option,


value="option1")

radio_button2 = tk.Radiobutton(root, text="Option 2", variable=selected_option,


value="option2")
from tkinter import *
master = Tk()
master.geometry("175x175")
# Tkinter string variable able to store any string value
v = StringVar(master, "1")
# Dictionary to create multiple buttons
values = {"RadioButton 1" : "1",
"RadioButton 2" : "2",
"RadioButton 3" : "3",
"RadioButton 4" : "4",
"RadioButton 5" : "5"}
for (text, value) in values.items():
Radiobutton(master, text = text, variable = v,value = value, indicator = 0,
background = "light blue").pack(fill = X, ipady = 5)
mainloop()
OUTPUT
Message

The Message widget is used to display multiple lines of text in a formatted manner. It
provides a way to present longer messages or paragraphs of text within a tkinter window or
frame.

SYNTAX:

message = tk.Message(root, text="This is a multiline message.")


Scale

• The Scale widget is used to create a slider control that allows the user to select a value
within a specified range. It provides a way to interactively adjust and control numeric
values.

SYNTAX:

• scale = tk.Scale(root, from_=0, to=100, orient=tk.HORIZONTAL)


Scrollbar

• The Scrollbar widget plays a crucial role in providing an interactive scrolling experience to
the user, ensuring easy access to the complete content of a widget.The Scrollbar widget is
used to provide scrolling functionality to other widgets that display more content than
their visible area can accommodate.

SYNTAX:

• scrollbar = tk.Scrollbar(root)
Text

• Text widget provides a way to display and edit multiline text content. It is a versatile
widget that allows the user to input and manipulate text data.

• The Text widget offers several features and functionalities, making it useful in various
scenarios.
Toplevel

• Top-level window is a separate, independent window that serves as a top-level container


for other widgets. It is commonly used to create additional windows or dialog boxes that
appear on top of the main application window.

SYNTAX:

• top_window = tk.Toplevel(root)
Spinbox

• Spinbox widget is used to provide a numerical input field with increment and decrement
buttons. It allows the user to select a value by either typing directly into the widget or by
using the up and

• down buttons to increase or decrease the value.

SYNTAX:

• spinbox = tk.Spinbox(root, from_=0, to=100)


PanedWindow

• PanedWindow widget is used to create a container that allows the user to resize and
adjust the size of its child widgets using a splitter or divider. It provides a way to create
resizable and adjustable panes within a window or frame.

SYNTAX:

• paned_window = tk.PanedWindow(root, orient=tk.HORIZONTAL)


Label Frame

• LabelFrame widget is used to create a container that groups and organizes other widgets
together. It provides a labeled border around its content, making it visually distinct and
allowing for better organization of related widgets.

SYNTAX:

• label_frame = tk.LabelFrame(root, text="My LabelFrame")


Message Box

• MessageBox module provides a convenient way to display various types of message


boxes or dialog boxes to the user. These message boxes are commonly used to show
informational messages, warnings, errors, or to prompt for user input.

SYNTAX:

• import tkinter as tk

• from tkinter import messagebox


Database introduction

• A database is a structured collection of data that is organized and stored in a way that
allows for efficient retrieval, management, and manipulation of information. It is
designed to store and manage large volumes of data, providing mechanisms for data
storage, retrieval, modification, and deletion.
Mysql

• MySQL is an open-source relational database management system (RDBMS) that is


widely used for managing and organizing structured data. It is a popular choice for web
applications, business systems, and other data-intensive applications.
Database creation

• In MySQL, data creation involves creating a database, defining tables with appropriate
columns and data types, and inserting data into those tables.

SYNTAX:

• CREATE DATABASE your_database_name;


Table creation - DDL queries

• In MySQL, table creation and definition are performed using Data Definition Language
(DDL) queries. DDL queries allow you to create, modify, and delete database objects such
as tables, indexes, and constraints.

• CREATE TABLE your_table_name (

id INT PRIMARY KEY,

name VARCHAR(50),

age INT,

email VARCHAR(100));
• DML queries

DML (Data Manipulation Language) queries in MySQL are used to manipulate and retrieve data stored within tables.
DML queries allow you to insert, update, delete, and retrieve data from the database.

• INSERT:

INSERT INTO your_table_name (column1, column2, ...)

VALUES (value1, value2, ...);

• UPDATE:

UPDATE your_table_name

SET column1 = value1, column2 = value2, ...

WHERE condition;

DELETE:

DELETE FROM your_table_name

• WHERE condition;
• Application with database connectivity

• An application with database connectivity refers to a software application that interacts


with a database to store, retrieve, and manipulate data. The application uses a database
management system (DBMS) to establish a connection with the database and perform
various operations on the data.

You might also like