Updated - Project 2 - GUI
Updated - Project 2 - GUI
Objectives
1. Introduce GUI
2. Describe how to install tkinter
3. Explore the GUI tkinter library
- 209 -
1.1 Introduction
import tkinter
When you run the previous application, you’ll observe that the
position of the window located at the top left side of the screen.
Label
window.title('Hello Python')
window.geometry("400x300")
L= Label(window, text="Username")
L.pack()
window.mainloop()
The first parameter of the Label call is the name of the parent
window, in our case "window". So, our Label widget is a child
of the window widget. The keyword parameter "text" specifies
the text to be shown. The pack() method tells Tk to fit the size
of the window to the given text. Our script will remain in the
event loop until we close the window.
Parameters:
• master: This represents the parent window
Add image
from tkinter import *
window=Tk()
# add widgets here
window.title('Hello Python')
window.geometry("400x300")
logo = PhotoImage(file="bis.png")
L1= Label(window, text="Username",
image=logo)
L1.pack()
L2= Label(window, text="Business
Information Systems")
L2.pack()
Entry
Syntax
Parameters
Example:
Button
Syntax
Example
output
Example
top = Tk()
L1 = Label(top, text="User Name")
L1.pack( side = LEFT)
E1 = Entry(top, bd =5)
E1.pack(side = RIGHT)
B1=Button(top,text="Submit")
B1.pack(side = BOTTOM)
top.mainloop()
Frame
Syntax
Example
frame3 = Frame(top)
frame3.pack( side = BOTTOM )
L1 = Label(frame1, text="User Name")
L1.pack()
E1 = Entry(frame2, bd =5)
E1.pack()
L2 = Label(frame1, text="Password")
L2.pack()
E2 = Entry(frame2, bd =5)
E2.pack()
B1=Button(frame3,text="Submit")
B1.pack()
top.mainloop()
Check Button
Syntax
Radio button
Syntax
Example
frame1 = Frame(top)
frame1.pack(side = LEFT)
frame2 = Frame(top)
frame2.pack( side = RIGHT )
frame3 = Frame(top)
frame3.pack( side = BOTTOM )
frame4 = Frame(top)
frame4.pack( side = BOTTOM )
L1 = Label(frame1, text="First Number")
L1.pack()
E1 = Entry(frame2, bd =5)
E1.pack()
L2 = Label(frame1, text="Second Number")
L2.pack()
E2 = Entry(frame2, bd =5)
E2.pack()
L3 = Label(frame1, text="Result")
L3.pack()
E3 = Entry(frame2, bd =5)
E3.pack()
B1=Button(frame4,text="+")
B1.pack(side = LEFT)
B2=Button(frame4,text="-")
B2.pack(side = LEFT)
B3=Button(frame4,text="/")
B3.pack(side = RIGHT)
B4=Button(frame4,text="*")
B4.pack(side = RIGHT)
B5=Button(frame3,text="Exit")
B5.pack(side = LEFT)
B6=Button(frame3,text="Clear")
B6.pack(side = RIGHT)
top.mainloop()
Syntax:
Parameters:
There are various parameters:
• Function_Name: This parameter is used to represents an
appropriate message box function.
• title: This parameter is a string which is shown as a title of
a message box.
• message: This parameter is the string to be displayed as a
message on the message box.
• options: There are two options that can be used are:
1. default: This option is used to specify the default
button like ABORT, RETRY, or IGNORE in the
message box.
2. parent: This option is used to specify the window on
top of which the message box is to be displayed.
Function_Name:
There are functions or methods available in the messagebox
widget.
1. showinfo(): Show some relevant information to the
user.
2. showwarning(): Display the warning to the user.
3. showerror(): Display the error message to the user.
4. askquestion(): Ask question and user has to answered
in yes or no.
root = Tk()
root.geometry("300x200")
w = Label(root, text='Test massagebox',
font="50")
w.pack()
messagebox.showinfo("showinfo",
"Information")
messagebox.showwarning("showwarning",
"Warning")
messagebox.showerror("showerror", "Error")
messagebox.askquestion("askquestion", "Are
you sure?")
messagebox.askokcancel("askokcancel",
"Want to continue?")
messagebox.askyesno("askyesno", "Find the
value?")
messagebox.askretrycancel("askretrycancel"
, "Try again?")
root.mainloop()
Output
Example
win.geometry("700x350")
def show_msg():
showinfo("Message","Hey There! I hope
you are doing well.")
Label(win, text= "Welcome BIS!", font=
('Aerial 17 bold italic')).pack(pady= 30)
Button(win, text= "Click Here",
command=show_msg).pack(pady= 20)
win.mainloop()
Output
Example:
import tkinter as tk
def New_Window():
window = tk.Tk()
window.title("Python Main Window")
window.geometry('400x300')
L = tk.Label(window, text="Welcome to
Python Second window")
L.pack()
ws = tk.Tk()
ws.title("Python Second Window")
ws.geometry('400x300')
L1 = tk.Label(ws, text="Welcome to Python
Main Window")
L1.pack()
button = tk.Button(ws, text="Click ME",
bg='White', fg='Black', command=lambda:
New_Window())
button.pack()
ws.mainloop()
top = Tk()
top.title(string='Press A Button')
top.geometry('400x300')
def java():
output_label.config(text='You Pressed
Java Button')
def python():
output_label.config(text='You Pressed
Python Button')
B1=Button(top,text="Java", command=java)
B1.pack()
B2=Button(top,text="Python",
command=python)
B2.pack()
output_label = Label(top)
output_label.pack()
top.mainloop()
top = Tk()
top.title(string='User Information')
top.geometry('400x500')
def user():
name=E1.get()
password=E2.get()
email=E3.get()
output_text="You Registered Successfully
\n Name: "+name+"\n Password:
"+password+"\n Email: "+email
output_label.config(text=output_text)
logo = PhotoImage(file="bis.png")
L= Label(top, text="Username", image=logo)
L.pack()
L1 = Label(top, text="Enter your Name")
L1.pack()
E1 = Entry()
E1.pack()
L2 = Label(top, text="Enter your Password")
L2.pack()
E2 = Entry()
E2.pack()
L3 = Label(top, text="Enter your E-mail")
L3.pack()
E3 = Entry()
E3.pack()
B1=Button(top,text="Submit", command=user)
B1.pack()
output_label = Label(top)
output_label.pack()
top.mainloop()
BMI= weight/(height*height)
top = Tk()
top.title(string='Compute BMI')
top.geometry('400x300')
def bmi():
w=float(E1.get())
h =float(E2.get())
bmi=w/(h*h)
if bmi<16:
output_text="You are seriously
Underweight"
elif bmi<18:
output_text="You are Underweight"
elif bmi < 24:
output_text = "You are Normal
weight"
elif bmi < 29:
def clear():
E1.delete(0,'end')
E2.delete(0,'end')
output_label.config(text='')
top = Tk()
top.title(string='Guess A Number')
top.geometry('400x300')
random = r.randint(0, 100)
def guess():
num=int(E1.get())
if num>100 or num<0:
output_text="Your number should be
between 0 to 100"
elif num==random:
output_text="Congratulations You
are Won"
elif num<random:
output_text = "Your number is less
than random number"
else:
output_text = "Your number is
greater than random number"
output_label.config(text=output_text)
def clear():
E1.delete(0,'end')
output_label.config(text='')
B2=Button(top,text="Clear", command=clear)
B2.pack()
output_label = Label(top)
output_label.pack()
top.mainloop()
top = Tk()
top.title(string='Add Numbers')
top.geometry('400x300')
def add():
f=float(E1.get())
s=float(E2.get())
r=f+s
output_text = "The result of Addition is
"+str(r)
output_label.config(text=output_text)
def clear():
E1.delete(0,'end')
E2.delete(0,'end')
output_label.config(text='')
B3=Button(top,text="Clear", command=clear)
B3.pack()
output_label = Label(top)
output_label.pack()
top.mainloop()
top = Tk()
top.title(string='Simple Calculator')
top.geometry('400x300')
def add():
result=int(E1.get())+int(E2.get())
E3.insert(0,result)
def sub():
result=int(E1.get())-int(E2.get())
E3.insert(0,result)
def div():
result=int(E1.get())/int(E2.get())
E3.insert(0,result)
def mlt():
result=int(E1.get())*int(E2.get())
E3.insert(0,result)
def exit():
top.quit()
def clear():
E1.delete(0, 'end')
E2.delete(0, 'end')
E3.delete(0, 'end')
frame1 = Frame(top)
frame1.pack(side = LEFT)
frame2 = Frame(top)
frame2.pack( side = RIGHT )
frame3 = Frame(top)
frame3.pack( side = BOTTOM )
frame4 = Frame(top)
frame4.pack( side = BOTTOM )
1.7 Menus
The menu can have multiple sub menus and each sub menu can
contain items. Menu items can be associated with callback
methods, meaning when you click them a Python method is
called.
import tkinter as tk
from tkinter import Menu
from tkinter import Frame
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
menu = Menu(self.master)
self.master.config(menu=menu)
fileMenu = Menu(menu)
fileMenu.add_command(label="Item")
fileMenu.add_command(label="Exit",
command=self.exitProgram)
menu.add_cascade(label="File",
menu=fileMenu)
editMenu = Menu(menu)
editMenu.add_command(label="Undo")
editMenu.add_command(label="Redo")
menu.add_cascade(label="Edit",
menu=editMenu)
def exitProgram(self):
exit()
First, create a root window and set its title to 'Menu Demo':
root = tk.Tk()
root.title('Menu Demo')
menubar = Menu(root)
root.config(menu=menubar)
Note that each top-level window can only have only one menu
bar.
file_menu = Menu(menubar)
file_menu.add_command(
label='Exit',
command=root.destroy,
)
When you click the Exit menu item, Python will call
the root.destroy() method automatically to close
the root window.
menubar.add_cascade(
label="File",
menu=file_menu,
underline=0
)
import tkinter as tk
from tkinter import Menu
# root window
root = tk.Tk()
root.title('Menu Demo')
# create a menubar
menubar = Menu(root)
root.config(menu=menubar)
# create a menu
file_menu = Menu(menubar)
root.mainloop()
Output:
# concatenation of string
expression = expression + str(num)
global expression
equation.set(total)
def clear():
global expression
expression = ""
equation.set("")
# Driver code
if __name__ == "__main__":
# create a GUI window
gui = Tk()
fg='black', bg='red',
command=lambda:
press(9), height=1, width=7)
button9.grid(row=4, column=2)
Chapter Summary
Notes
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
Notes
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------