0% found this document useful (0 votes)
37 views3 pages

123

This document defines code to create a basic calculator application using Tkinter in Python. It creates a window and entry field to display numbers. It defines button widgets for numbers 0-9 and mathematical operations like + - * / =. The buttons are placed in a grid layout and assigned click functions to add numbers, perform operations, clear, and calculate results. The entry field is updated based on button clicks to display input and results.

Uploaded by

AndeRR Eblan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views3 pages

123

This document defines code to create a basic calculator application using Tkinter in Python. It creates a window and entry field to display numbers. It defines button widgets for numbers 0-9 and mathematical operations like + - * / =. The buttons are placed in a grid layout and assigned click functions to add numbers, perform operations, clear, and calculate results. The entry field is updated based on button clicks to display input and results.

Uploaded by

AndeRR Eblan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import tkinter # Import Tkinter

# Creating a window
root = tkinter.Tk()

enter = tkinter.Entry(root, width=40, font=('Arial', 9),


borderwidth=4, relief=tkinter.SUNKEN, bg='#f7f7f7')
enter.grid(column=0, row=0, columnspan=3, padx=3, pady=3)

# Buttons 0-9
button_0 = tkinter.Button(root, text="0", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(0))
button_1 = tkinter.Button(root, text="1", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(1))
button_2 = tkinter.Button(root, text="2", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(2))
button_3 = tkinter.Button(root, text="3", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(3))
button_4 = tkinter.Button(root, text="4", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(4))
button_5 = tkinter.Button(root, text="5", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(5))
button_6 = tkinter.Button(root, text="6", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(6))
button_7 = tkinter.Button(root, text="7", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(7))
button_8 = tkinter.Button(root, text="8", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(8))
button_9 = tkinter.Button(root, text="9", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(9))

# Buttons for mathematical operations


button_add = tkinter.Button(root, text="+", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_operation("+"))
button_subtract = tkinter.Button(
root, text="-", padx=30, pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_operation("-"))
button_multiply = tkinter.Button(
root, text="*", padx=30, pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_operation("*"))
button_divide = tkinter.Button(
root, text="/", padx=30, pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_operation("/"))
button_equal = tkinter.Button(
root, text="=", padx=30, pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_equal())
button_clear = tkinter.Button(
root, text="C", padx=30, pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_clear())

# A Decimial button
button_decimal = tkinter.Button(
root, text=".", padx=30, pady=20, font=('Arial', 10, 'bold'), command=lambda:
button_decimal())

# Place buttons 0-9 w/ grid


button_0.grid(row=4, column=0)
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)

# Place buttons for mathematical operations w/ grid


button_add.grid(row=1, column=3)
button_subtract.grid(row=2, column=3)
button_multiply.grid(row=3, column=3)
button_divide.grid(row=4, column=3)
button_equal.grid(row=4, column=2)
button_clear.grid(row=0, column=3)

# Place a decimal button


button_decimal.grid(row=4, column=1)

# Function to add a number

def button_click(number):
current = enter.get()
enter.delete(0, tkinter.END)
enter.insert(0, str(current) + str(number))

# Function to clear enter box

def button_clear():
enter.delete(0, tkinter.END)

# Function for a decimal button

def button_decimal():
current = enter.get()
if "." not in current:
enter.delete(0, tkinter.END)
enter.insert(0, str(current) + ".")

# Function to add a mathematicaloperation

def button_operation(operation):
global math_operation
math_operation = operation
global first_number
first_number = float(enter.get())
enter.delete(0, tkinter.END)

# Function for equal button

def button_equal():
second_number = float(enter.get())
enter.delete(0, tkinter.END)
if math_operation == "+":
enter.insert(0, first_number + second_number)
elif math_operation == "-":
enter.insert(0, first_number - second_number)
elif math_operation == "*":
enter.insert(0, first_number * second_number)
elif math_operation == "/":
enter.insert(0, first_number / second_number)

root.mainloop() # Mainloop window

You might also like