Python Tkinter Calculate 2 Numbers - Addition Subtraction Multiplication Division

How To Calculate 2 Numbers Using Python And Tkinter

Python Tkinter Calculate 2 Numbers

In This Python Tkinter Tutorial we will See How To Calculate 2 Numbers ( Sum, Mutiplication, Division, Substraction ) Using Input Text, Button And Select Option (combobox) In Python Using Tkinter and VsCode Editor.

Source Code:


import tkinter as tk
from tkinter import *
from tkinter import ttk


root = Tk()

root.title("Python Tkinter Calculate 2 Numbers")

frame = tk.Frame(root, bg='#45aaf2')

entry_n1 = tk.Entry(frame, font=('verdana',16), width=10)
entry_n2 = tk.Entry(frame, font=('verdana',16), width=10)

options = ['+','-','x','/']
selected = StringVar(frame)
selected.set(options[0])

combobox = ttk.Combobox(frame, textvariable=selected, values=options,
font=('verdana',16), width=2)

btn = tk.Button(frame, text='=',bg='grey', fg='#ffffff', font=('verdana',12), width=2)

entry_result = tk.Entry(frame, font=('verdana',16), width=10)


def calculate():
num1 = int(entry_n1.get())
num2 = int(entry_n2.get())
opr = combobox.get()
if opr == '+':
result = num1 + num2
elif opr == '-':
result = num1 - num2
elif opr == 'x':
result = num1 * num2
elif opr == '/':
result = num1 / num2
entry_result.delete(0,END)
entry_result.insert(0,result)


btn['command'] = calculate


frame.pack()

entry_n1.grid(row=0, column=0, columnspan="3", padx=5, pady=5, sticky="w")
entry_n2.grid(row=1, column=0, columnspan="3", padx=5, pady=5, sticky="w")
combobox.grid(row=2, column=0, padx=5)
btn.grid(row=2, column=1, padx=5, pady=5)
entry_result.grid(row=2, column=2, padx=5)

root.mainloop()


OUTPUT:

Python Tkinter Addition, Subtraction, Multiplication, Division
Python Tkinter Addition, Subtraction, Multiplication, Division

Python Tkinter Addition
Python Tkinter Addition

Python Tkinter Subtraction
Python Tkinter Subtraction

Python Tkinter Multiplication
Python Tkinter Multiplication

Python Tkinter Division
Python Tkinter Division






Share this

Related Posts

Previous
Next Post »