3.1 Python Tkinter Programs
3.1 Python Tkinter Programs
&
VUI
Programs
mhmeducations.com
Tutorial 1
mhmeducations.com
Simple Program
from tkinter import *
root = Tk()
mainloop()
mhmeducations.com
Window of 400*400 Pixel
from tkinter import *
root = Tk()
root.geometry("400x400")
mainloop()
mhmeducations.com
Window with Title
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("400x400")
mainloop()
mhmeducations.com
Window with Message(Label)
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("400x400")
my_label=Label(root, text='IT Skill Training')
my_label.pack()
mainloop()
mhmeducations.com
Window with Message
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("400x400")
my_label=Label(root, text='IT Skill Training')
my_label.pack()
my_label=Label(root, text='mhmeducations.com')
my_label.pack()
mainloop()
mhmeducations.com
Front ground (fg) and Back ground(bg)
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("400x400")
my_label=Label(root, text='IT Skill Training', fg='blue')
my_label.pack()
my_label=Label(root, text='mhmeducations.com', bg='white')
my_label.pack()
mainloop()
mhmeducations.com
Tutorial 2
mhmeducations.com
Width and Height and space
between two sentence (pady)
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("400x400")
my_label=Label(root, text='IT Skill Training', fg='blue',bg='white', width=50)
my_label.pack()
my_label=Label(root, text='mhmeducations.com', bg='white',height=10)
my_label.pack(pady=50)
mainloop()
mhmeducations.com
Check changing it
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("400x400")
my_label=Label(root, text='IT Skill Training', relief='sunken')
my_label.pack()
my_label=Label(root, text='mhmeducations.com', font=('Helvtica',30))
my_label.pack(pady=50)
mainloop()
mhmeducations.com
Check changing it
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("400x400")
my_label=Label(root, text='IT Skill Training', relief='raised')
my_label.pack()
my_label=Label(root, text='mhmeducations.com', font=('Helvtica',30))
my_label.pack(pady=50)
mainloop()
mhmeducations.com
Check changing it
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("400x400")
my_label=Label(root, text='IT Skill Training', relief='groove')
my_label.pack()
my_label=Label(root, text='mhmeducations.com', font=('Helvtica',30))
my_label.pack(pady=50)
mainloop()
mhmeducations.com
Tutorial 3
mhmeducations.com
grid
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("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='mhmeducations.com')
my_label.grid(row=0,column=1)
mainloop() mhmeducations.com
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("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='mhmeducations.com')
my_label.grid(row=1,column=0)
mainloop()
mhmeducations.com
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("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='mhmeducations.com')
my_label.grid(row=1,column=0, sticky=W)
mainloop()
mhmeducations.com
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("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='mhmeducations.com')
my_label.grid(row=1,column=0, sticky=E)
mainloop()
mhmeducations.com
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("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='mhmeducations.com')
my_label.grid(row=1,column=1)
my_label=Label(root, text='MHM is best')
my_label.grid(row=2,column=2)
mainloop() mhmeducations.com
Tutorial 4
mhmeducations.com
Button
mhmeducations.com
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("400x400")
def clicked():
my_label2 = Label(root, text="mhmeducations.com")
my_label2.pack()
# Create Buttons
my_button = Button(root, text="Click Me!", command=clicked)
my_button.pack(pady=20)
mainloop() mhmeducations.com
Entry Widget
mhmeducations.com
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("400x400")
def clicked(): # Create clicked function
input = e.get()
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))
e.pack(pady=20)
my_button = Button(root, text="Click Me!", command=clicked) # Create Buttons
my_button.pack(pady=20)
mainloop()
Add Image
mhmeducations.com
from tkinter import *
from PIL import ImageTk, Image (# install pillow)
root = Tk()
root.title("Hello World!")
root.geometry("400x900")
# Add Images
my_image = ImageTk.PhotoImage(Image.open("logo.png"))
image_label = Label(image=my_image)
image_label.pack()
mhmeducations.com
Show and Hide button
mhmeducations.com
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("400x400")
def clicked():
global my_label2
my_label2 = Label(root, text="mhmeducations.com")
my_label2.pack()
def hide():
my_label2.pack_forget()
def show():
my_label2.pack()
mhmeducations.com
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("400x400")
def fake_command():
pass
my_menu = Menu(root)
root.config(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=root.quit)
root.mainloop()
Tutorial 5
mhmeducations.com
Status bar
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("400x400")
root.mainloop()
mhmeducations.com
Radio button
Program in two slide
mhmeducations.com
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("400x400")
def radio():
if v.get() == "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()
root.mainloop()
mhmeducations.com
Check box
mhmeducations.com
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("400x400")
v = StringVar()
root.mainloop()
mhmeducations.com
Tutorial 6
mhmeducations.com
Pop up
mhmeducations.com
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title("MHM")
root.geometry("400x400")
def popup():
response = messagebox.showinfo("mhm", "mhmeducations.com")
my_label = Label(root, text=response).pack(pady=10)
root.mainloop()
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title("MHM")
root.geometry("400x400")
def popup():
response = messagebox. askyesno("mhm", "mhmeducations.com")
my_label = Label(root, text=response).pack(pady=10)
root.mainloop()
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title("MHM")
root.geometry("400x400")
def popup():
response = messagebox. askokcancel("mhm", "mhmeducations.com")
my_label = Label(root, text=response).pack(pady=10)
root.mainloop()
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title("MHM")
root.geometry("400x400")
def popup():
response = messagebox. askquestion("mhm", "mhmeducations.com")
my_label = Label(root, text=response).pack(pady=10)
root.mainloop()
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title("MHM")
root.geometry("400x400")
def popup():
response = messagebox. showerror("mhm", "mhmeducations.com")
my_label = Label(root, text=response).pack(pady=10)
root.mainloop()
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title("MHM")
root.geometry("400x400")
def popup():
response = messagebox. showwarning("mhm", "mhmeducations.com")
my_label = Label(root, text=response).pack(pady=10)
root.mainloop()
Tutorial 7
mhmeducations.com
Combo Box
mhmeducations.com
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry("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"]
mhmeducations.com
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.geometry("400x400")
def open_window():
new = Toplevel()
new.title("Second Window")
new.geometry("500x700")
my_label = Label(new, text="second window!").pack(pady=20)
destroy_button = Button(new, text="Quit", command=new.destroy)
destroy_button.pack(pady=5)
kill_original = Button(new, text="Quit Original", command=root.destroy).pack()
new.mainloop()
mhmeducations.com
from tkinter import *
from tkinter import colorchooser
root = Tk()
root.title("Hello World!")
root.geometry("400x400")
def color():
my_color = colorchooser.askcolor()[1]
my_label = Label(root, text=my_color).pack()
my_label2 = Label(root, text="You Picked A Color!!",
bg=my_color).pack()
root.mainloop() mhmeducations.com
Panel
mhmeducations.com
pane1 = PanedWindow(bd=4, relief="raised", bg="red")
pane1.pack(fill=BOTH, expand=1)
mainloop()
Tutorial 8
mhmeducations.com
Canvas widget used to draw simple
shapes to complicated graphs
mhmeducations.com
#The Canvas widget used to draw simple shapes to complicated graphs
root = Tk()
C.pack()
root.mainloop()
mhmeducations.com
Menu widget
mhmeducations.com
from tkinter import *
from tkinter.ttk import *
mhmeducations.com
from tkinter import *
root = Tk()
root.geometry("300x300")
root.mainloop()
mhmeducations.com
Progessbar
mhmeducations.com
from tkinter import * progress['value'] = 90
from tkinter.ttk import *
root = Tk() root.update_idletasks()
time.sleep(2)
# Progress bar widget
progress = Progressbar(root, orient = progress['value'] = 100
HORIZONTAL, length = 100)
root.update_idletasks()
# Function for progress bar value time.sleep(2)
def probar():
import time
progress['value'] = 10 progress.pack()
root.update_idletasks()
time.sleep(2)
# the progress bar
progress['value'] = 50 Button(root, text = 'Click here', command =
root.update_idletasks() probar).pack()
time.sleep(2)
mainloop()
mhmeducations.com
Toplevel Widget
mhmeducations.com
from tkinter import *
root = Tk()
root.geometry("300x400")
root.title("main")
top = Toplevel()
top.geometry("200x200")
top.title("TOP LEVEL")
l2 = Label(top, text = "This is TOPLEVEL window")
l1.pack()
l2.pack()
top.mainloop() mhmeducations.com
Tutorial 9
mhmeducations.com
File Frame
• Program in two slide
mhmeducations.com
from tkinter import *
root = Tk()
root.title("MHM")
root.geometry("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)
root.config(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=root.quit)
root.mainloop()
Entry Widgets with Grid
Program in two slide
mhmeducations.com
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.geometry("400x400")
hello_label = Label(root)
#Submit Function
def submit():
global hello_label
clear()
hello_label = Label(root, text="Hello " + e.get())
hello_label.grid(row=3, column=0)
mhmeducations.com
#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)
e.grid(row=1, column=0)
root.mainloop()
mhmeducations.com
Tutorial 10
mhmeducations.com
destroy() method
mhmeducations.com
from tkinter import *
from tkinter.ttk import *
root = Tk()
mainloop() mhmeducations.com
askopenfile() function
mhmeducations.com
from tkinter import *
from tkinter.ttk import *
from tkinter.filedialog import askopenfile
root = Tk()
root.geometry('300x300')
def open_file():
file = askopenfile(mode ='r', filetypes =[('Python Files', '*.py')])
if file is not None:
content = file.read()
print(content)
mainloop()
mhmeducations.com
bind function:- it is universal widget
method
mhmeducations.com
from tkinter import *
from tkinter.ttk import *
root = Tk()
root.geometry('300x300')
def enter(event):
print('Curser is on Window')
def exit_(event):
print('Curser is not on window')
mainloop() mhmeducations.com
Tutorial 11
mhmeducations.com
asksaveasfile() function
mhmeducations.com
from tkinter import *
from tkinter import ttk
from tkinter.filedialog import asksaveasfile
root = Tk()
root.geometry('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)
mainloop() mhmeducations.com
MessageBox Widget
mhmeducations.com
from tkinter import *
from tkinter import messagebox
root = Tk()
root.geometry("300x200")
messagebox.askokcancel("Want to continue?")
messagebox.showerror("Error is generator")
root.mainloop()
mhmeducations.com
Hierarchical treeview in GUI application
mhmeducations.com
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry("400x400")
treeview.insert('', '0', 'item1', text ='Main Heading') # Inserting parent or root of tree
mhmeducations.com
from tkinter import *
root = Tk()
root.geometry("300x300")
v1 = DoubleVar()
def readscale():
sel = "Horizontal Scale Value = " + str(v1.get())
l1.config(text = sel)
s1.pack()
l3.pack()
b1.pack()
l1.pack()
root.mainloop() mhmeducations.com
Scale Widget:- Vertical Scale
mhmeducations.com
from tkinter import *
root = Tk()
root.geometry("300x300")
v1 = DoubleVar()
def readscale():
sel = "Horizontal Scale Value = " + str(v1.get())
l1.config(text = sel)
s1.pack()
l3.pack()
b1.pack()
l1.pack()
root.mainloop() mhmeducations.com
Tutorial 12
mhmeducations.com
focus_set() and focus_get() method
mhmeducations.com
from tkinter import *
from tkinter.ttk import *
root = Tk()
e1 = Entry(root)
e1.pack(expand = 1, fill = BOTH)
mhmeducations.com
from tkinter import *
from tkinter.ttk import *
root = Tk()
def forget(widget):
widget.forget()
def retrieve(widget):
widget.pack(fill = BOTH, expand = True)
my_label=Label(root, text='mhmeducations.com')
my_label.pack(fill = BOTH, expand = True)
mainloop()
mhmeducations.com
forget_grid() method
mhmeducations.com
from tkinter import *
from tkinter.ttk import *
root = Tk()
def forget(widget):
widget.grid_forget()
def retrieve(widget):
widget.grid(row = 0, column = 0)
my_label=Label(root, text='mhmeducations.com')
my_label.grid(row = 0, column = 0)
mainloop()
mhmeducations.com
after method in Tkinter
• Destroying Window
mhmeducations.com
from tkinter import *
from tkinter.ttk import *
from time import time
root = Tk()
start = time()
mainloop()
end = time()
print('Destroyed after % d seconds' % (end-start)) mhmeducations.com
after method in Tkinter
• Prompting Window
mhmeducations.com
from tkinter import *
from tkinter.ttk import *
from time import time
from tkinter.messagebox import _show
root = Tk()
start = time()
mainloop()
end = time()
mhmeducations.com
Speech to Text
mhmeducations.com
#Install following
• pip install speechrecognition
• pip install wheel
• pip install pyttsx3
• pip install pipwin
• pipwin install pyaudio
mhmeducations.com
import speech_recognition as sr
import pyttsx3
mhmeducations.com
# Write infinite loop for user to speak
while True:
try:
mhmeducations.com
Text to Speech
mhmeducations.com
#Install following
• pip install pyttsx3
mhmeducations.com
#text to speech
#Install following
#pip install pyttsx3
import pyttsx3
# entered text There will be a pause between each one like a pause in a sentence
converter.say("Hello how are you")
converter.say("I am Piyush Dave")
converter.runAndWait() # Program will not continue until all speech is done talking
mhmeducations.com
Speech Recognition in Python
using Google Speech API
mhmeducations.com
import speech_recognition as sr
AUDIO_FILE = ("Record Example.wav") # use the audio file as the audio source
r = sr.Recognizer()
try:
print("The Text from audio file are: " + r.recognize_google(audio))
except sr.UnknownValueError:
print("Audio is not Clear")
except sr.RequestError as e:
print("Try Again; {0}".format(e))
mhmeducations.com