Hello friends,
In this python tutorial for beginners video, we are going to create simple GUI calculator application using python tkinter module
Requirement; install tkinter module with command 'pip install tkinter'
Adding the code part used in video session, please comment your inputs and more topics
Code starts
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
canvas1 = tk.Canvas(root,width=300,height=300)
canvas1.pack()
def calc():
num1 = num1_entry.get()
num2 = num2_entry.get()
operator = opt_entry.get()
if operator == '+':
add = int(num1) + int(num2)
tk.messagebox.showinfo('Addition',add)
elif operator == '-':
sub = int(num1) - int(num2)
tk.messagebox.showinfo('Subtraction',sub)
print("The sub value is ", sub)
elif operator == '/':
div = int(num1) / int(num2)
tk.messagebox.showinfo('Division',div)
elif operator == '*':
mul = int(num1) * int(num2)
tk.messagebox.showinfo('Multiply',mul)
else:
tk.messagebox.showinfo('Alert','Wrong Operator')
root.destroy()
#header label
header_label = tk.Label(root,text="Welcome to Calculator")
canvas1.create_window(165,35,window=header_label)
#num1 input field
num1_entry = tk.Entry(root,text='Num1')
canvas1.create_window(215,75,window=num1_entry)
num1_label = tk.Label(root,text='Enter Num1 Value :')
canvas1.create_window(95,75,window=num1_label)
#num2 input field
num2_entry = tk.Entry(root,text='Num2')
canvas1.create_window(215,105,window=num2_entry)
num2_label = tk.Label(root,text='Enter Num2 Value :')
canvas1.create_window(95,105,window=num2_label)
#operator input field
opt_entry = tk.Entry(root,text='Opt')
canvas1.create_window(215,135,window=opt_entry)
opt_label = tk.Label(root,text='Select Operator :')
canvas1.create_window(95,135,window=opt_label)
#calc button
calc_button = tk.Button(root,text='Calculate',command=calc)
canvas1.create_window(215,165,window=calc_button)
root.mainloop()
Code Ends
python tutorial for beginners
python exercises
python learning
simply python application
tkinter python module
On this page of the site you can watch the video online Python Exercise #6 | Create GUI calculator application using Python Tkinter module | Technolazy with a duration of hours minute second in good quality, which was uploaded by the user Technolazy 27 July 2020, share the link with friends and acquaintances, this video has already been watched 270 times on youtube and it was liked by 5 viewers. Enjoy your viewing!