How to Create Calculator in Python with GUI

Calculator in Python with GUI

To create a calculator in Python with a graphical user interface (GUI), you will need to use a GUI library. There are several options available for Python, such as Tkinter, PyQt, and PyGTK.

Here is an example of how you could create a simple calculator using Tkinter, which is a built-in GUI library in Python:

import tkinter as tk

# Create the main window
window = tk.Tk()
window.title("Calculator")

# Create the input and output fields
input_field = tk.Entry(window, width=35)
input_field.grid(row=0, column=0, columnspan=3)
output_field = tk.Label(window, text="")
output_field.grid(row=1, column=0, columnspan=3)

# Create the buttons
button1 = tk.Button(window, text="1", width=5)
button1.grid(row=2, column=0)
button2 = tk.Button(window, text="2", width=5)
button2.grid(row=2, column=1)
button3 = tk.Button(window, text="3", width=5)
button3.grid(row=2, column=2)
button4 = tk.Button(window, text="4", width=5)
button4.grid(row=3, column=0)
button5 = tk.Button(window, text="5", width=5)
button5.grid(row=3, column=1)
button6 = tk.Button(window, text="6", width=5)
button6.grid(row=3, column=2)
button7 = tk.Button(window, text="7", width=5)
button7.grid(row=4, column=0)
button8 = tk.Button(window, text="8", width=5)
button8.grid(row=4, column=1)
button9 = tk.Button(window, text="9", width=5)
button9.grid(row=4, column=2)
button0 = tk.Button(window, text="0", width=5)
button0.grid(row=5, column=1)
button_add = tk.Button(window, text="+", width=5)
button_add.grid(row=2, column=3)
button_subtract = tk.Button(window, text="-", width=5)
button_subtract.grid(row=3, column=3)
button_multiply = tk.Button(window, text="*", width=5)
button_multiply.grid(row=4, column=3)
button_divide = tk.Button(window, text="/", width=5)
button_divide.grid(row=5, column=3)
button_equal = tk.Button(window, text="=", width=5)
button_equal.grid(row=5, column=2)
button_clear = tk.Button(window, text="Clear", width=5)
button_clear.grid(row=5, column=0)

# Create the button event handlers
def button_click(number):
    current = input

How to create a Calculator in Python with Code