Python Tkinter (GUI)
&
VUI
Programs
[Link]
Tutorial 1
[Link]
Simple Program
from tkinter import *
root = Tk()
mainloop()
[Link]
Window of 400*400 Pixel
from tkinter import *
root = Tk()
[Link]("400x400")
mainloop()
[Link]
Window with Title
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
mainloop()
[Link]
Window with Message(Label)
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
my_label=Label(root, text='IT Skill Training')
my_label.pack()
mainloop()
[Link]
Window with Message
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
my_label=Label(root, text='IT Skill Training')
my_label.pack()
my_label=Label(root, text='[Link]')
my_label.pack()
mainloop()
[Link]
Front ground (fg) and Back ground(bg)
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
my_label=Label(root, text='IT Skill Training', fg='blue')
my_label.pack()
my_label=Label(root, text='[Link]', bg='white')
my_label.pack()
mainloop()
[Link]
Tutorial 2
[Link]
Width and Height and space
between two sentence (pady)
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
my_label=Label(root, text='IT Skill Training', fg='blue',bg='white', width=50)
my_label.pack()
my_label=Label(root, text='[Link]', bg='white',height=10)
my_label.pack(pady=50)
mainloop()
#pady=50 mean 50 pixel down
[Link]
Font and size
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
my_label=Label(root, text='IT Skill Training', fg='blue',bg='white',
width=50)
my_label.pack()
my_label=Label(root, text='[Link]', font=('Helvtica',30))
my_label.pack(pady=50)
mainloop()
[Link]
Check changing it
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
my_label=Label(root, text='IT Skill Training', relief='sunken')
my_label.pack()
my_label=Label(root, text='[Link]', font=('Helvtica',30))
my_label.pack(pady=50)
mainloop()
[Link]
Check changing it
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
my_label=Label(root, text='IT Skill Training', relief='raised')
my_label.pack()
my_label=Label(root, text='[Link]', font=('Helvtica',30))
my_label.pack(pady=50)
mainloop()
[Link]
Check changing it
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
my_label=Label(root, text='IT Skill Training', relief='groove')
my_label.pack()
my_label=Label(root, text='[Link]', font=('Helvtica',30))
my_label.pack(pady=50)
mainloop()
[Link]
Tutorial 3
[Link]
grid
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
my_label=Label(root, text='IT Skill Training', bg="white", width=50)
my_label.grid(row=0,column=0)
my_label=Label(root, text='[Link]')
my_label.grid(row=0,column=1)
mainloop() [Link]
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
my_label=Label(root, text='IT Skill Training',
bg="white", width=50)
my_label.grid(row=0,column=0)
my_label=Label(root, text='[Link]')
my_label.grid(row=1,column=0)
mainloop()
[Link]
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
my_label=Label(root, text='IT Skill Training',
bg="white", width=50)
my_label.grid(row=0,column=0)
my_label=Label(root, text='[Link]')
my_label.grid(row=1,column=0, sticky=W)
mainloop()
[Link]
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
my_label=Label(root, text='IT Skill Training',
bg="white", width=50)
my_label.grid(row=0,column=0)
my_label=Label(root, text='[Link]')
my_label.grid(row=1,column=0, sticky=E)
mainloop()
[Link]
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
my_label=Label(root, text='IT Skill Training',
bg="white", width=50)
my_label.grid(row=0,column=0)
my_label=Label(root, text='[Link]')
my_label.grid(row=1,column=1)
my_label=Label(root, text='MHM is best')
my_label.grid(row=2,column=2)
mainloop() [Link]
Tutorial 4
[Link]
Button
[Link]
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
# Create clicked function
def clicked():
my_label2 = Label(root, text="[Link]")
my_label2.pack()
# Create Buttons
my_button = Button(root, text="Click Me!", command=clicked)
my_button.pack(pady=20)
mainloop() [Link]
Entry Widget
[Link]
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
def clicked(): # Create clicked function
input = [Link]()
my_label2 = Label(root, text="Hello " + input)
my_label2.pack()
my_label = Label(root, text='Enter Your Name:') # Create labels
my_label.pack()
#Create Entry Widget Input Box
e = Entry(root, font=("Helvetica", 18))
[Link](pady=20)
my_button = Button(root, text="Click Me!", command=clicked) # Create Buttons
my_button.pack(pady=20)
mainloop()
Add Image
[Link]
from tkinter import *
from PIL import ImageTk, Image (# install pillow)
root = Tk()
[Link]("Hello World!")
[Link]("400x900")
# Add Images
my_image = [Link]([Link]("[Link]"))
image_label = Label(image=my_image)
image_label.pack()
[Link]
Show and Hide button
[Link]
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
def clicked():
global my_label2
my_label2 = Label(root, text="[Link]")
my_label2.pack()
def hide():
my_label2.pack_forget()
def show():
my_label2.pack()
my_button = Button(root, text="Click Me!", command=clicked)
my_button.pack(pady=20)
hide_button = Button(root, text="Hide", command=hide)
hide_button.pack(pady=20)
show_button = Button(root, text="Show", command=show)
show_button.pack(pady=20)
Toolbar
[Link]
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
def fake_command():
pass
my_menu = Menu(root)
[Link](menu=my_menu)
file_menu = Menu(my_menu)
my_menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=fake_command)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=[Link])
[Link]()
Tutorial 5
[Link]
Status bar
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
my_status = Label(root, text="Waiting", bd=2, relief="sunken", anchor=W, width=400)
my_status.grid(row=2, column=0, sticky=S)
[Link]()
[Link]
Radio button
Program in two slide
[Link]
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
def radio():
if [Link]() == "one":
my_label = Label(root, text="You Clicked Radio Button One!")
else:
my_label = Label(root, text="You Clicked Radio Button Two!")
my_label.pack(pady=10)
v = StringVar()
rbutton_1 = Radiobutton(root, text="One", variable=v,
value="one").pack()
rbutton_2 = Radiobutton(root, text="Two", variable=v,
value="two").pack()
my_button = Button(root, text="Click Me", command=radio)
my_button.pack(pady=20)
[Link]()
[Link]
Check box
[Link]
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
v = StringVar()
my_check = Checkbutton(root, text="MHM", variable=v)
my_check.deselect()
my_check.pack()
[Link]()
[Link]
Tutorial 6
[Link]
Pop up
[Link]
from tkinter import *
from tkinter import messagebox
root = Tk()
[Link]("MHM")
[Link]("400x400")
def popup():
response = [Link]("mhm", "[Link]")
my_label = Label(root, text=response).pack(pady=10)
pop_button = Button(root, text="Click To Pop Up!", command=popup)
pop_button.pack(pady=20)
[Link]()
from tkinter import *
from tkinter import messagebox
root = Tk()
[Link]("MHM")
[Link]("400x400")
def popup():
response = messagebox. askyesno("mhm", "[Link]")
my_label = Label(root, text=response).pack(pady=10)
pop_button = Button(root, text="Click To Pop Up!", command=popup)
pop_button.pack(pady=20)
[Link]()
from tkinter import *
from tkinter import messagebox
root = Tk()
[Link]("MHM")
[Link]("400x400")
def popup():
response = messagebox. askokcancel("mhm", "[Link]")
my_label = Label(root, text=response).pack(pady=10)
pop_button = Button(root, text="Click To Pop Up!", command=popup)
pop_button.pack(pady=20)
[Link]()
from tkinter import *
from tkinter import messagebox
root = Tk()
[Link]("MHM")
[Link]("400x400")
def popup():
response = messagebox. askquestion("mhm", "[Link]")
my_label = Label(root, text=response).pack(pady=10)
pop_button = Button(root, text="Click To Pop Up!", command=popup)
pop_button.pack(pady=20)
[Link]()
from tkinter import *
from tkinter import messagebox
root = Tk()
[Link]("MHM")
[Link]("400x400")
def popup():
response = messagebox. showerror("mhm", "[Link]")
my_label = Label(root, text=response).pack(pady=10)
pop_button = Button(root, text="Click To Pop Up!", command=popup)
pop_button.pack(pady=20)
[Link]()
from tkinter import *
from tkinter import messagebox
root = Tk()
[Link]("MHM")
[Link]("400x400")
def popup():
response = messagebox. showwarning("mhm", "[Link]")
my_label = Label(root, text=response).pack(pady=10)
pop_button = Button(root, text="Click To Pop Up!", command=popup)
pop_button.pack(pady=20)
[Link]()
Tutorial 7
[Link]
Combo Box
[Link]
from tkinter import *
from tkinter import ttk
root = Tk()
[Link]("400x400")
def select():
if my_combo.get() == "Monday":
my_label = Label(root, text="Monday!").pack(pady=10)
if my_combo.get() == "Tuesday":
my_label = Label(root, text="Tuesday!").pack(pady=10)
options = ["Monday","Tuesday"]
my_combo = [Link](root, value=options)
my_combo.current(0)
my_combo.pack(pady=10)
my_button = Button(root, text="Select", command=select).pack()
[Link]()
New Window
[Link]
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
[Link]("400x400")
def open_window():
new = Toplevel()
[Link]("Second Window")
[Link]("500x700")
my_label = Label(new, text="second window!").pack(pady=20)
destroy_button = Button(new, text="Quit", command=[Link])
destroy_button.pack(pady=5)
kill_original = Button(new, text="Quit Original", command=[Link]).pack()
[Link]()
my_button = Button(root, text="Open 2nd Window", command=open_window).pack()
[Link]()
Choose a color
[Link]
from tkinter import *
from tkinter import colorchooser
root = Tk()
[Link]("Hello World!")
[Link]("400x400")
def color():
my_color = [Link]()[1]
my_label = Label(root, text=my_color).pack()
my_label2 = Label(root, text="You Picked A Color!!",
bg=my_color).pack()
my_button = Button(root, text="Pick A Color", command=color).pack()
[Link]() [Link]
Panel
[Link]
pane1 = PanedWindow(bd=4, relief="raised", bg="red")
[Link](fill=BOTH, expand=1)
left = Label(pane1, text="left pane")
[Link](left)
pane2 = PanedWindow(pane1, orient=VERTICAL, bd=4, relief="raised", bg="blue")
[Link](pane2)
top = Label(pane2, text="top pane")
[Link](top)
bottom = Label(pane2, text="bottom pane")
[Link](bottom)
mainloop()
Tutorial 8
[Link]
Canvas widget used to draw simple
shapes to complicated graphs
[Link]
#The Canvas widget used to draw simple shapes to complicated graphs
from tkinter import *
root = Tk()
C = Canvas(root, bg ="purple", height = 350, width = 350)
line = C.create_line(250, 150, 350, 250, fill ="red")
arc = C.create_arc(200, 180, 100,230, start = 0, extent = 250, fill ="blue")
oval = C.create_oval(100, 50, 160, 170, fill ="yellow")
[Link]()
[Link]()
[Link]
Menu widget
[Link]
from tkinter import *
from [Link] import *
root = Tk() # creat tkinter window
[Link]('Menu Demonstration')
menubar = Menu(root) # Creat Menubar
file = Menu(menubar) # Adding Menu
menubar.add_cascade(label ='File', menu = file)
file.add_command(label ='New', command = None)
file.add_separator()
file.add_command(label ='Exit', command = [Link])
help_ = Menu(menubar) # Adding Menu
menubar.add_cascade(label ='Help', menu = help_)
help_.add_command(label ='Demo', command = None)
help_.add_separator()
help_.add_command(label ='About Tk', command = None)
[Link](menu = menubar) # display Menu
mainloop() [Link]
Spinbox Widgets
[Link]
from tkinter import *
root = Tk()
[Link]("300x300")
w = Label(root, text ='mhm', font = "100")
[Link]()
sp = Spinbox(root, from_= 0, to = 50)
[Link]()
[Link]()
[Link]
Progessbar
[Link]
from tkinter import * progress['value'] = 90
from [Link] import *
root = Tk() root.update_idletasks()
[Link](2)
# Progress bar widget
progress = Progressbar(root, orient = progress['value'] = 100
HORIZONTAL, length = 100)
root.update_idletasks()
# Function for progress bar value [Link](2)
def probar():
import time
progress['value'] = 10 [Link]()
root.update_idletasks()
[Link](2)
# the progress bar
progress['value'] = 50 Button(root, text = 'Click here', command =
root.update_idletasks() probar).pack()
[Link](2)
mainloop()
[Link]
Toplevel Widget
[Link]
from tkinter import *
root = Tk()
[Link]("300x400")
[Link]("main")
l1 = Label(root, text = "This is main window")
top = Toplevel()
[Link]("200x200")
[Link]("TOP LEVEL")
l2 = Label(top, text = "This is TOPLEVEL window")
[Link]()
[Link]()
[Link]() [Link]
Tutorial 9
[Link]
File Frame
• Program in two slide
[Link]
from tkinter import *
root = Tk()
[Link]("MHM")
[Link]("400x400")
def fake_command():
pass
def new():
hide_menu_frames()
file_frame.grid(row=1, column=0, columnspan=2, padx=20, pady=20)
def fake_command():
pass
def new():
file_frame.grid(row=1, column=0, columnspan=2, padx=20, pady=20)
my_menu = Menu(root)
[Link](menu=my_menu)
#Create Menu Items
file_menu = Menu(my_menu)
my_menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=new)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=[Link])
# File Menu Frame
file_frame = Frame(root, width=400, height=400, bd=5, bg="blue", relief="sunken")
file_frame_label = Label(file_frame, text="File Frame", font=("Helvetica", 20))
file_frame_label.pack(padx=20, pady=20)
[Link]()
Entry Widgets with Grid
Program in two slide
[Link]
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
[Link]("400x400")
hello_label = Label(root)
#Submit Function
def submit():
global hello_label
clear()
hello_label = Label(root, text="Hello " + [Link]())
hello_label.grid(row=3, column=0)
[Link]
#Create clear function
def clear():
hello_label.grid_forget()
# Forget
my_label = Label(root, text="Enter Your Name:").grid(row=0, column=0)
e = Entry(root)
[Link](row=1, column=0)
my_button = Button(root, text="Submit", command=submit).grid(row=2, column=0)
clear_button = Button(root, text="Clear", command=clear).grid(row=2, column=1)
[Link]()
[Link]
Tutorial 10
[Link]
destroy() method
[Link]
from tkinter import *
from [Link] import *
root = Tk()
b1 = Button(root, text ="Button 1", command = [Link])
# window will be destroyed
[Link](pady = 50)
b2 = Button(root, text ="Button 2", command = [Link])
# button 1 will be destroyed
[Link](pady = 50)
mainloop() [Link]
askopenfile() function
[Link]
from tkinter import *
from [Link] import *
from [Link] import askopenfile
root = Tk()
[Link]('300x300')
def open_file():
file = askopenfile(mode ='r', filetypes =[('Python Files', '*.py')])
if file is not None:
content = [Link]()
print(content)
b1 = Button(root, text ='Open', command = lambda:open_file())
[Link](side = TOP, pady = 50)
mainloop()
[Link]
bind function:- it is universal widget
method
[Link]
from tkinter import *
from [Link] import *
root = Tk()
[Link]('300x300')
def enter(event):
print('Curser is on Window')
def exit_(event):
print('Curser is not on window')
frame = Frame(root, height = 250, width = 250)
[Link]('<Enter>', enter) #working of bind function
[Link]('<Leave>', exit_)
[Link]()
mainloop() [Link]
Tutorial 11
[Link]
asksaveasfile() function
[Link]
from tkinter import *
from tkinter import ttk
from [Link] import asksaveasfile
root = Tk()
[Link]('300x300')
def save():
files = [('All Files', '*.*'), # open and ask to save file
('Python Files', '*.py'),
('Text Doc', '*.txt'),
('Image file', '*.jpg')]
file = asksaveasfile(filetypes = files, defaultextension = files)
b1 = [Link](root, text = 'Save', command = lambda : save())
[Link](side = TOP, pady = 20)
mainloop() [Link]
MessageBox Widget
[Link]
from tkinter import *
from tkinter import messagebox
root = Tk()
[Link]("300x200")
[Link]("Are you sure?")
[Link]("Want to continue?")
[Link]("are you born in July?")
[Link]("Do you want to Try again?")
[Link]("Information about installation")
[Link]("Don't Ingore Warning")
[Link]("Error is generator")
[Link]()
[Link]
Hierarchical treeview in GUI application
[Link]
from tkinter import *
from tkinter import ttk
root = Tk()
[Link]("400x400")
[Link]("GUI Application wirh TreeView")
[Link](root, text ="Hierarchical Treeview").pack()
treeview = [Link](root) # Create treeview window
[Link]()
[Link]('', '0', 'item1', text ='Main Heading') # Inserting parent or root of tree
[Link]('', '1', 'item2', text ='Sub Heading1') # Inserting child or branches
[Link]('', '2', 'item3', text ='Sub Heading2')
[Link]('item2', 'end', text ='Product 1') #inserting Leaf or items
[Link]('item2', 'end', text ='Product 2')
[Link]('item3', 'end', text ='Product 1')
[Link]('item2', 'item1', 'end') # Join Parents and child
[Link]('item3', 'item1', 'end')
[Link]
[Link]()
Scale Widget:- Horizontal Scale
[Link]
from tkinter import *
root = Tk()
[Link]("300x300")
v1 = DoubleVar()
def readscale():
sel = "Horizontal Scale Value = " + str([Link]())
[Link](text = sel)
s1 = Scale( root, variable = v1,from_ = 1, to = 1000, orient = HORIZONTAL)
l3 = Label(root, text = "Scale")
b1 = Button(root, text ="Display the Reading",command = readscale, bg = "orange")
l1 = Label(root)
[Link]()
[Link]()
[Link]()
[Link]()
[Link]() [Link]
Scale Widget:- Vertical Scale
[Link]
from tkinter import *
root = Tk()
[Link]("300x300")
v1 = DoubleVar()
def readscale():
sel = "Horizontal Scale Value = " + str([Link]())
[Link](text = sel)
s1 = Scale( root, variable = v1,from_ = 1, to = 1000, orient = VERTICAL)
l3 = Label(root, text = "Scale")
b1 = Button(root, text ="Display the Reading",command = readscale, bg = "orange")
l1 = Label(root)
[Link]()
[Link]()
[Link]()
[Link]()
[Link]() [Link]
Tutorial 12
[Link]
focus_set() and focus_get() method
[Link]
from tkinter import *
from [Link] import *
root = Tk()
def focus(event): #Name the Widgets which is Focus
widget = root.focus_get()
print(widget, "has focus")
e1 = Entry(root)
[Link](expand = 1, fill = BOTH)
e2 = Button(root, text ="Button on Focus")
[Link](pady = 10)
e2.focus_set() #Make Button on Focus
e3 = Radiobutton(root, text ="MHM")
[Link](pady = 10)
root.bind_all("<Button-1>", lambda e: focus(e))
mainloop()
[Link]
forget_pack() method
[Link]
from tkinter import *
from [Link] import *
root = Tk()
def forget(widget):
[Link]()
def retrieve(widget):
[Link](fill = BOTH, expand = True)
b1 = Button(root, text = "Forget Label", command = lambda : forget(my_label))
[Link](fill = BOTH, expand = True)
b2 = Button(root, text = "Retrive Label", command = lambda : retrieve(my_label))
[Link](fill = BOTH, expand = True)
my_label=Label(root, text='[Link]')
my_label.pack(fill = BOTH, expand = True)
mainloop()
[Link]
forget_grid() method
[Link]
from tkinter import *
from [Link] import *
root = Tk()
def forget(widget):
widget.grid_forget()
def retrieve(widget):
[Link](row = 0, column = 0)
my_label=Label(root, text='[Link]')
my_label.grid(row = 0, column = 0)
b1 = Button(root, text = "Forget Label", command = lambda : forget(my_label))
[Link](row = 0, column = 1)
b2 = Button(root, text = "Retrive Label", command = lambda : retrieve(my_label))
[Link](row = 0, column = 2)
mainloop()
[Link]
after method in Tkinter
• Destroying Window
[Link]
from tkinter import *
from [Link] import *
from time import time
root = Tk()
button = Button(root, text = 'Destroy')
[Link]()
print('Its Running Now............')
start = time()
[Link](10000, [Link]) #10000 miliseconds means 10 Second
mainloop()
end = time()
print('Destroyed after % d seconds' % (end-start)) [Link]
after method in Tkinter
• Prompting Window
[Link]
from tkinter import *
from [Link] import *
from time import time
from [Link] import _show
root = Tk()
button = Button(root, text = 'New Window')
[Link]()
print('Its Running Now............')
start = time()
[Link](10000, lambda : _show('Title', 'Prompting after 10 seconds'))
mainloop()
end = time()
[Link]
Speech to Text
[Link]
#Install following
• pip install speechrecognition
• pip install wheel
• pip install pyttsx3
• pip install pipwin
• pipwin install pyaudio
[Link]
import speech_recognition as sr
import pyttsx3
r = [Link]() # recognizer initialization
def SpeakText(command): # convert text to speech
engine = [Link]() #engine initialization
[Link](command)
[Link]()
[Link]
# Write infinite loop for user to speak
while True:
try:
with [Link]() as source2: # use the microphone as source for input
r.adjust_for_ambient_noise(source2, duration=0.2) #listens to user's input
audio2 = [Link](source2) # Using google to recognize audio
MyText = r.recognize_google(audio2)
MyText = [Link]()
print(“You Might Said "+MyText)
SpeakText(MyText)
except [Link] as e:
print("Could not request results; {0}".format(e))
except [Link]:
print(“Speak Again")
[Link]
Text to Speech
[Link]
#Install following
• pip install pyttsx3
[Link]
#text to speech
#Install following
#pip install pyttsx3
import pyttsx3
converter = [Link]() # Initialize the converter
[Link]('rate', 200) # Sets speed percent
[Link]('volume', 1) # Set volume 0-1
# entered text There will be a pause between each one like a pause in a sentence
[Link]("Hello how are you")
[Link]("I am Piyush Dave")
[Link]() # Program will not continue until all speech is done talking
[Link]
Speech Recognition in Python
using Google Speech API
[Link]
import speech_recognition as sr
AUDIO_FILE = ("Record [Link]") # use the audio file as the audio source
r = [Link]()
with [Link](AUDIO_FILE) as source:
audio = [Link](source)
try:
print("The Text from audio file are: " + r.recognize_google(audio))
except [Link]:
print("Audio is not Clear")
except [Link] as e:
print("Try Again; {0}".format(e))
[Link]