# Day 13
# _____ _ ___
# /__ \_____ _| |_ / __\ _____ _____ ___
# / /\/ _ \ \/ / __| /__\/// _ \ \/ / _ \/ __|
# / / | __/> <| |_ / \/ \ (_) > < __/\__ \
# \/ \___/_/\_\\__| \_____/\___/_/\_\___||___/ and frames
from tkinter import *
# window
window =Tk()
window.resizable(True, True)
window.title("Gui! with textboxes")
window.geometry("400x250+200+200")
# Function
# _,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_
# calculating | make button input an x value to do them all in one fuction (SO MUCH EASIER!)
def calculate(x):
try:
s1=float(box1.get())
s2=float(box2.get())
if x == "+":
vsum = s1+s2
elif x == "-":
vsum = s1-s2
elif x == "x":
vsum = s1*s2
elif x == "/":
try:
vsum = s1/s2
except ZeroDivisionError: # error checking for n/0
vsum = "ERROR"
elif x == "^":
try:
vsum = s1**s2
except ZeroDivisionError: # error checking for 0^-n
vsum = "ERROR"
elif x == "√":
try:
vsum = s1**(1/s2)
except ZeroDivisionError: # error checking for zero root
vsum = "ERROR"
except ValueError:
vsum="Invalid answer"
result.config(text= vsum)
# Create ui
# _,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_
# Header
header = Label(window, text="white boy can calculate", font=("Helvetica", 25), foreground = "blue")
header.pack()
# Frames (for organizing ui) | when making a wiget, put it in the frame instead of window (so much easier then placing it manually)
first_number_frame = Frame(window)
first_number_frame.pack(pady=10)
second_number_frame = Frame(window)
second_number_frame.pack(pady=10)
operator_frame = Frame(window)
operator_frame.pack(pady=10)
# Labels
# box one label
textboxlabel1=Label(first_number_frame, text="insert first number here -->", font = ("Helvetica", 12))
textboxlabel1.pack(side="left")
# box two label
textboxlabel2=Label(second_number_frame, text="insert second number here -->", font = ("Helvetica", 12))
textboxlabel2.pack(side="left")
# result label
resultlabel=Label(window, text="RESULT", font = ("Helvetica", 12))
resultlabel.pack()
# result
result=Label(window, text="", font = ("Helvetica", 15), foreground = "red")
result.pack()
# Operator Buttons
# Add
add = Button(operator_frame, text="+", background="blue", foreground="white", font=20, command = lambda : calculate("+"))
add.pack(padx=10, side="left")
# Subtract
subtract = Button(operator_frame, text="-", background="blue", foreground="white", font=20, command = lambda : calculate("-"))
subtract.pack(padx=10, side="left")
# Multiple
multiple = Button(operator_frame, text="x", background="blue", foreground="white", font=20, command = lambda : calculate("x"))
multiple.pack(padx=10, side="left")
# divide
divide = Button(operator_frame, text="/", background="blue", foreground="white", font=20, command = lambda : calculate("/"))
divide.pack(padx=10, side="left")
# exponent
exponent = Button(operator_frame, text="^", background="blue", foreground="white", font=20, command = lambda : calculate("^"))
exponent.pack(padx=10, side="left")
# root
root = Button(operator_frame, text="√", background="blue", foreground="white", font=20, command = lambda : calculate("√"))
root.pack(padx=10, side="left")
# entrys (text boxes)
# box1
box1 = Entry(first_number_frame, bd=2, width=20)
box1.pack(side="right")
# box2
box2 = Entry(second_number_frame, bd=2, width=20)
box2.pack(side="right")
# _,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_