Simple Calculator using Tkinter
import tkinter as tk
root = tk.Tk()
root.title('BASIC CALCULATOR')
root.geometry('640x480+300+300')
root.resizable(False, False)
title = tk.Label(
root,
text='Simple Calculator',
font=('Timesnewroman 18 bold'),
bg='green',
fg='#AB1'
)
var1=tk.DoubleVar()
var2=tk.DoubleVar()
var1_label = tk.Label(root, text='Number1: ')
var1_inp = tk.Entry(root, textvariable=var1)
var2_label = tk.Label(root, text='Number: ')
var2_inp = tk.Entry(root, textvariable=var2)
plus_btn = tk.Button(root, text=' + ')
minus_btn = tk.Button(root, text=' - ')
mul_btn=tk.Button(root,text='*')
divide_btn=tk.Button(root,text='/')
output_var = tk.StringVar(value='')
output_line = tk.Label(
root,
textvariable=output_var,
anchor='w',
justify='left'
)
title.grid(columnspan=4)
var1_label.grid(row=10,column=0)
var1_inp.grid(row=10,column=1)
var2_label.grid(row=10,column=2)
var2_inp.grid(row=10,column=3)
plus_btn.grid(row=99,column=0)
minus_btn.grid(row=99,column=1)
mul_btn.grid(row=99,column=2)
divide_btn.grid(row=99,column=3)
output_line.grid(row=100, columnspan=2, sticky='NSEW')
root.columnconfigure(1, weight=1)
root.rowconfigure(99, weight=2)
root.rowconfigure(100, weight=1)
def on_plus_submit():
"""To be run when the user submits the form"""
val1 = var1.get()
val2=var2.get()
mysum=val1+val2
message = f'sum is , {mysum}.\n'
output_var.set(message)
def on_minus_submit():
"""To be run when the user submits the form"""
val1 = var1.get()
val2=var2.get()
minus=val1-val2
message = f'difference is , {minus}.\n'
output_var.set(message)
def on_mul_submit():
"""To be run when the user submits the form"""
val1 = var1.get()
val2=var2.get()
mul=val1*val2
message = f'multiplication is , {mul}.\n'
output_var.set(message)
def on_divide_submit():
"""To be run when the user submits the form"""
val1 = var1.get()
val2=var2.get()
divide=val1/val2
message = f'division is , {divide}.\n'
output_var.set(message)
plus_btn.configure(command=on_plus_submit)
minus_btn.configure(command=on_minus_submit)
mul_btn.configure(command=on_mul_submit)
divide_btn.configure(command=on_divide_submit)
root.mainloop()
Comments
Post a Comment