Skip to content

Add new button in calculator. #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions calc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import tkinter as tk
from tkinter import font

from matplotlib.pyplot import text

LARGE_FONT_STYLE = ("Arial", 40, "bold")
SMALL_FONT_STYLE = ("Arial", 16)
Expand Down Expand Up @@ -53,6 +56,7 @@ def bind_keys(self):

def create_special_buttons(self):
self.create_clear_button()
self.create_clear_last_character_button()
self.create_equals_button()
self.create_square_button()
self.create_sqrt_button()
Expand Down Expand Up @@ -104,14 +108,24 @@ def clear(self):
self.update_label()
self.update_total_label()

def clear_last_character(self):
if self.current_expression != "":
self.current_expression = self.current_expression[:-1]
self.update_label()

def create_clear_button(self):
button = tk.Button(self.buttons_frame, text="C", bg=OFF_WHITE, fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE,
borderwidth=0, command=self.clear)
button.grid(row=0, column=1, sticky=tk.NSEW)

def create_clear_last_character_button(self):
button = tk.Button(self.buttons_frame,text="<=",bg=OFF_WHITE,fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE,
borderwidth=0,command=self.clear_last_character )
button.grid(row=4,column=3,sticky=tk.NSEW)

def square(self):
self.current_expression = str(eval(f"{self.current_expression}**2"))
self.update_label()
self.update_label()

def create_square_button(self):
button = tk.Button(self.buttons_frame, text="x\u00b2", bg=OFF_WHITE, fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE,
Expand Down Expand Up @@ -142,7 +156,7 @@ def evaluate(self):
def create_equals_button(self):
button = tk.Button(self.buttons_frame, text="=", bg=LIGHT_BLUE, fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE,
borderwidth=0, command=self.evaluate)
button.grid(row=4, column=3, columnspan=2, sticky=tk.NSEW)
button.grid(row=4, column=4, columnspan=1, sticky=tk.NSEW)

def create_buttons_frame(self):
frame = tk.Frame(self.window)
Expand All @@ -165,3 +179,4 @@ def run(self):
if __name__ == "__main__":
calc = Calculator()
calc.run()
calc.clear_last_character()