Hello everyone, In this post, you will learn how to create GUI for simple arithmetic application using Tkinter in Python. The sample code has been tested and shared in the same post.

Example Program

import tkinter as tk
# creating an instance for Tk
root = tk.Tk()
res = None
# setting a title
root.title("Application")
# Label : First Number
l1 = tk.Label(root, text = "First Number")
l1.pack()
# Text : First Number
t1 = tk.Text(root, height = 1, width = 30)
t1.pack()
# Label : Second Number
l2 = tk.Label(root, text = "Second Number")
l2.pack()
# Text : Second Number
t2 = tk.Text(root, height = 1, width = 30)
t2.pack()
# defining add function
def add():
a = int(t1.get("1.0", 'end-1c'))
b = int(t2.get("1.0", 'end-1c'))
res.config(text = (a + b))
# defining subtract function
def subtract():
a = int(t1.get("1.0", 'end-1c'))
b = int(t2.get("1.0", 'end-1c'))
res.config(text = (a - b))
# defining multiply function
def multiply():
a = int(t1.get("1.0", 'end-1c'))
b = int(t2.get("1.0", 'end-1c'))
res.config(text = (a * b))
# defining divide function
def divide():
a = int(t1.get("1.0", 'end-1c'))
b = int(t2.get("1.0", 'end-1c'))
res.config(text = (a / b))
# Button : Add
add = tk.Button(root, text = "Add", command = add)
add.pack()
# Button : Subtract
subtract = tk.Button(root, text = "Subtract", command = subtract)
subtract.pack()
# Button : Multiply
multiply = tk.Button(root, text = "Multiply", command = multiply)
multiply.pack()
# Button : Divide
divide = tk.Button(root, text = "Divide", command = divide)
divide.pack()
# Label : Result
res = tk.Label(root, text = "")
res.pack()
# starting a window
root.mainloop()

Output

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *