Import tkinter-WPS Office
Import tkinter-WPS Office
OFF_WHITE = "#F8FAFF"
WHITE = "#FFFFFF"
LIGHT_BLUE = "#CCEDFF"
LIGHT_GRAY = "#F5F5F5"
LABEL_COLOR = "#25265E"
class Calculator:
def __init__(self):
self.window = tk.Tk()
self.window.geometry("375x667")
self.window.resizable(0, 0)
self.window.title("Calculator")
self.total_expression = ""
self.current_expression = ""
self.display_frame = self.create_display_frame()
self.total_label, self.label = self.create_display_labels()
self.digits = {
self.buttons_frame = self.create_buttons_frame()
self.buttons_frame.rowconfigure(0, weight=1)
self.buttons_frame.rowconfigure(x, weight=1)
self.buttons_frame.columnconfigure(x, weight=1)
self.create_digit_buttons()
self.create_operator_buttons()
self.create_special_buttons()
self.bind_keys()
def bind_keys(self):
def create_special_buttons(self):
self.create_clear_button()
self.create_equals_button()
self.create_square_button()
self.create_sqrt_button()
def create_display_labels(self):
total_label.pack(expand=True, fill='both')
label.pack(expand=True, fill='both')
def create_display_frame(self):
frame.pack(expand=True, fill="both")
return frame
def add_to_expression(self, value):
self.current_expression += str(value)
self.update_label()
def create_digit_buttons(self):
self.current_expression += operator
self.total_expression += self.current_expression
self.current_expression = ""
self.update_total_label()
self.update_label()
def create_operator_buttons(self):
i=0
i += 1
def clear(self):
self.current_expression = ""
self.total_expression = ""
self.update_label()
self.update_total_label()
def create_clear_button(self):
borderwidth=0, command=self.clear)
def square(self):
self.current_expression = str(eval(f"{self.current_expression}**2"))
self.update_label()
def create_square_button(self):
borderwidth=0, command=self.square)
def sqrt(self):
self.current_expression = str(eval(f"{self.current_expression}**0.5"))
self.update_label()
def create_sqrt_button(self):
borderwidth=0, command=self.sqrt)
def evaluate(self):
self.total_expression += self.current_expression
self.update_total_label()
try:
self.current_expression = str(eval(self.total_expression))
self.total_expression = ""
except Exception as e:
self.current_expression = "Error"
finally:
self.update_label()
def create_equals_button(self):
borderwidth=0, command=self.evaluate)
def create_buttons_frame(self):
frame = tk.Frame(self.window)
frame.pack(expand=True, fill="both")
return frame
def update_total_label(self):
expression = self.total_expression
self.total_label.config(text=expression)
def update_label(self):
self.label.config(text=self.current_expression[:11])
def run(self):
self.window.mainloop()
if __name__ == "__main__":
calc = Calculator()
calc.run()