1+ import tkinter as tk
2+ from tkinter import ttk
3+ import math
4+
5+ class Calculator :
6+ def __init__ (self , root ):
7+ self .root = root
8+ self .root .title ("Calculator with History" )
9+ self .root .geometry ("600x400" )
10+ self .root .configure (bg = "#f0f0f0" )
11+
12+ self .current_expression = ""
13+ self .history = []
14+
15+ self .create_frames ()
16+
17+ self .create_display ()
18+
19+ self .create_buttons ()
20+
21+ self .create_history_display ()
22+
23+ def create_frames (self ):
24+
25+ self .main_container = ttk .Frame (self .root )
26+ self .main_container .pack (expand = True , fill = "both" , padx = 10 , pady = 10 )
27+
28+ self .calculator_frame = ttk .Frame (self .main_container )
29+ self .calculator_frame .pack (side = "left" , expand = True , fill = "both" , padx = (0 , 5 ))
30+
31+ self .history_frame = ttk .Frame (self .main_container )
32+ self .history_frame .pack (side = "right" , expand = True , fill = "both" , padx = (5 , 0 ))
33+
34+ def create_display (self ):
35+
36+ self .display = ttk .Entry (self .calculator_frame , justify = "right" , font = ("Arial" , 20 ))
37+ self .display .pack (fill = "x" , padx = 5 , pady = 5 )
38+
39+ def create_buttons (self ):
40+
41+ button_frame = ttk .Frame (self .calculator_frame )
42+ button_frame .pack (expand = True , fill = "both" )
43+
44+ buttons = [
45+ ('7' , 1 , 0 ), ('8' , 1 , 1 ), ('9' , 1 , 2 ), ('/' , 1 , 3 ),
46+ ('4' , 2 , 0 ), ('5' , 2 , 1 ), ('6' , 2 , 2 ), ('*' , 2 , 3 ),
47+ ('1' , 3 , 0 ), ('2' , 3 , 1 ), ('3' , 3 , 2 ), ('-' , 3 , 3 ),
48+ ('0' , 4 , 0 ), ('.' , 4 , 1 ), ('=' , 4 , 2 ), ('+' , 4 , 3 ),
49+ ('C' , 5 , 0 ), ('←' , 5 , 1 ), ('(' , 5 , 2 ), (')' , 5 , 3 ),
50+ ]
51+
52+ for (text , row , col ) in buttons :
53+ button = ttk .Button (button_frame , text = text , command = lambda t = text : self .button_click (t ))
54+ button .grid (row = row , column = col , sticky = "nsew" , padx = 2 , pady = 2 )
55+
56+ for i in range (6 ):
57+ button_frame .grid_rowconfigure (i , weight = 1 )
58+ for i in range (4 ):
59+ button_frame .grid_columnconfigure (i , weight = 1 )
60+
61+ def create_history_display (self ):
62+
63+ history_label = ttk .Label (self .history_frame , text = "History" , font = ("Arial" , 14 ))
64+ history_label .pack (pady = 5 )
65+
66+ self .history_listbox = tk .Listbox (self .history_frame , font = ("Arial" , 12 ))
67+ self .history_listbox .pack (expand = True , fill = "both" )
68+
69+ clear_history_btn = ttk .Button (self .history_frame , text = "Clear History" , command = self .clear_history )
70+ clear_history_btn .pack (pady = 5 )
71+
72+ def button_click (self , value ):
73+ if value == "=" :
74+ try :
75+ result = eval (self .current_expression )
76+
77+ history_entry = f"{ self .current_expression } = { result } "
78+ self .history .append (history_entry )
79+ self .history_listbox .insert (0 , history_entry )
80+
81+ self .current_expression = str (result )
82+ self .display .delete (0 , tk .END )
83+ self .display .insert (tk .END , self .current_expression )
84+ except :
85+ self .display .delete (0 , tk .END )
86+ self .display .insert (tk .END , "Error" )
87+ self .current_expression = ""
88+ elif value == "C" :
89+ self .current_expression = ""
90+ self .display .delete (0 , tk .END )
91+ elif value == "←" :
92+ self .current_expression = self .current_expression [:- 1 ]
93+ self .display .delete (0 , tk .END )
94+ self .display .insert (tk .END , self .current_expression )
95+ else :
96+ self .current_expression += value
97+ self .display .delete (0 , tk .END )
98+ self .display .insert (tk .END , self .current_expression )
99+
100+ def clear_history (self ):
101+ self .history = []
102+ self .history_listbox .delete (0 , tk .END )
103+
104+ if __name__ == "__main__" :
105+ root = tk .Tk ()
106+ app = Calculator (root )
107+ style = ttk .Style ()
108+ style .theme_use ('clam' )
109+ root .mainloop ()
0 commit comments