Step 1: Import Tkinter module
from tkinter import *
- This imports everything from Tkinter, which is used to create the graphical user interface (GUI).
Step 2: Create the Main Window
b = Tk()
b.minsize(650, 250)
b.maxsize(650, 250)
b.title("Arithmetic Operator")
b = Tk() creates the main window.
b.minsize(650, 250) sets the minimum window size to 650×250 pixels.
b.maxsize(650, 250) sets the maximum window size to 750×250 pixels.
b.title("Arithmetic Operator") sets the title of the window
Step 3: Define functions for arithmetic operations
Each function retrieves numbers from the entry fields, performs calculations, and displays the result.
Addition function
def add():
try:
num1 = float(e1.get())
num2 = float(e2.get())
answer = num1+num2
e3.delete(0, END)
e3.insert(0, str(answer))
except ValueError:
e3.delete(0, END)
e3.insert(0, "Invalid input")
Substraction function
def sub():
try:
num1 = float(e1.get())
num2 = float(e2.get())
answer = num1-num2
e3.delete(0, END)
e3.insert(0, str(answer))
except ValueError:
e3.delete(0, END)
e3.insert(0, "Invalid input")
Multiplication function
def mult():
try:
num1 = float(e1.get())
num2 = float(e2.get())
answer = num1*num2
e3.delete(0, END)
e3.insert(0, str(answer))
except ValueError:
e3.delete(0, END)
e3.insert(0, "Invalid input")
Division function
def div():
try:
num1 = float(e1.get())
num2 = float(e2.get())
if num2 ==0:
e3.delete(0, END)
e3.insert(0, "number can't divide by 0")
answer = num1/num2
e3.delete(0, END)
e3.insert(0, str(answer))
except ValueError:
e3.delete(0, END)
e3.insert(0, "Invalid input")
Step 4: Create Labels
These labels indicate where to enter numbers and where the answer will be displayed.
l1 = Label(b, text="1st number", font=("arial",15))
l1.place(x=10, y=20)
l2 = Label(b, text="2nd number", font=("arial",15))
l2.place(x=10, y=60)
l3 = Label(b, text="Answer", font=("arial",15))
l3.place(x=10, y=180)
Label() creates a label with text.
place(x, y) positions the label in the window.
Step 5: Create Entry Fields
Users enter numbers in these fields.
e1 = Entry(b, font=("arial",15))
e1.place(x=225, y=20)
e2 = Entry(b, font=("arial",15))
e2.place(x=225, y=60)
e3 = Entry(b, font=("arial",15))
e3.place(x=225, y=180)
Entry() creates an input box where the user can type numbers.
place(x, y) positions the entry fields.
Step 6: Create Buttons for Operations
Each button calls its respective function when clicked.
b1 = Button(b, text="add", font=("arial",15), command=add)
b1.place(x=50, y=120)
b2 = Button(b, text="subtract", font=("arial",15), command=sub)
b2.place(x=150, y=120)
b3 = Button(b, text="multiply", font=("arial",15), command=mult)
b3.place(x=320, y=120)
b4 = Button(b, text="divide", font=("arial",15), command=div)
b4.place(x=475, y=120)
Button() creates a clickable button.
command=add (or sub, mult, div) tells the button which function to execute when clicked.
place(x, y) positions the buttons.
Step 7: Run the Main Event Loop
b.mainloop()
- This starts the Tkinter event loop, keeping the window open.
Complete Code
from tkinter import *
b = Tk()
b.minsize(650, 250)
b.maxsize(750, 250)
b.title("Arithmetic Operator")
def add():
try:
num1 = float(e1.get())
num2 = float(e2.get())
answer = num1+num2
e3.delete(0, END)
e3.insert(0, str(answer))
except ValueError:
e3.delete(0, END)
e3.insert(0, "Invalid input")
def sub():
try:
num1 = float(e1.get())
num2 = float(e2.get())
answer = num1-num2
e3.delete(0, END)
e3.insert(0, str(answer))
except ValueError:
e3.delete(0, END)
e3.insert(0, "Invalid input")
def mult():
try:
num1 = float(e1.get())
num2 = float(e2.get())
answer = num1*num2
e3.delete(0, END)
e3.insert(0, str(answer))
except ValueError:
e3.delete(0, END)
e3.insert(0, "Invalid input")
def div():
try:
num1 = float(e1.get())
num2 = float(e2.get())
if num2 ==0:
e3.delete(0, END)
e3.insert(0, "number cannot divide by 0")
answer = num1/num2
e3.delete(0, END)
e3.insert(0, str(answer))
except ValueError:
e3.delete(0, END)
e3.insert(0, "Invalid input")
l1 = Label(b, text="1st number", font=("arial",15))
l1.place(x=10, y=20)
l2 = Label(b, text="2nd number", font=("arial",15))
l2.place(x=10, y=60)
l3 = Label(b, text="Answer", font=("arial",15))
l3.place(x=10, y=180)
e1 = Entry(b, font=("arial",15))
e1.place(x=225, y=20)
e2 = Entry(b, font=("arial",15))
e2.place(x=225, y=60)
e3 = Entry(b, font=("arial",15))
e3.place(x=225, y=180)
b1 = Button(b, text="add",font=("arial",15), command=add)
b1.place(x=50, y=120)
b2 = Button(b, text="subtract",font=("arial",15),command=sub)
b2.place(x=150, y=120)
b3 = Button(b, text="multiply",font=("arial",15), command=mult)
b3.place(x=320, y=120)
b4 = Button(b, text="divide",font=("arial",15),command=div)
b4.place(x=475, y=120)
b.mainloop()
Top comments (0)