import ttkbootstrap as ttk
from ttkbootstrap.constants import *
class Calculator(ttk.Frame):
def __init__(self,master,**kwargs):
super().__init__(master,padding=10,**kwargs)
ttk.Style().configure("TButton",font="TkFixedFont 12")
self.pack(fill=BOTH,expand=YES)
self.digitsvar=ttk.StringVar(value=0.0)
self.xnum=ttk.DoubleVar()
self.ynum=ttk.DoubleVar()
self.operator=ttk.StringVar(value='+')
if 'bootstyle' in kwargs:
self.bootstyle = kwargs.pop('bootstyle')
else:
self.bootstyle=None
self.creat_num_display()
self.creat_num_pad()
def creat_num_display(self):
container=ttk.Frame(master=self,padding=2,bootstyle=self.bootstyle)
container.pack(fill=X,pady=20)
digits=ttk.Label(
master=container,
font="TkFixedFont 14",
textvariable=self.digitsvar,
anchor=E,
)
digits.pack(fill=X)
def creat_num_pad(self):
container=ttk.Frame(master=self,padding=2,bootstyle=self.bootstyle)
container.pack(fill=BOTH,expand=YES)
matrix=[
("%","C","CE","/"),
("7","8","9","*"),
("4","5","6","-"),
("1","2","3","+"),
("+/-","0",".","="),
]
for i ,row in enumerate(matrix):
container.rowconfigure(i,weight=1)
for j,num_txt in enumerate(row):
container.columnconfigure(j,weight=1)
btn=self.creat_button(master=container,text=num_txt)
btn.grid(row=i,column=j,sticky=NSEW,padx=1,pady=1)
def creat_button(self,master,text):
if text== "=":
bootstyle = SUCCESS
elif not isinstance(text,int):
bootstyle = SECONDARY
else:
bootstyle = PRIMARY
return ttk.Button(
master=master,
text=text,
command=lambda x=text:self.on_button_pressed(x),
bootstyle=bootstyle,
width=2,
padding=10,
)
def on_button_pressed(self,txt):
display=self.digitsvar.get()
if len(display)>0:
if display[0] in ['/','*','-','+']:
display=display[1:]
if txt in ['CE','C']:
self.digitsvar.set('')
self.reset_variables()
elif isinstance(txt,int):
self.press_number(display,txt)
elif txt == '.' and "." not in display:
self.digitsvar.set(f'{display}{txt}')
elif txt=='+/-':
self.press_inverse(display)
elif txt in ["/","*","-","+"]:
self.press_operator(txt)
def reset_variables(self):
self.xnum.set(value=0)
self.ynum.set(value=0)
self.operator.set("+")
def press_number(self,diaplay,txt):
if diaplay == '0':
self.digitsvar.set(txt)
else:
self.digitsvar.set(f'{diaplay}{txt}')
def press_operator(self, txt):
self.operator.set(txt)
display=float(self.digitsvar.get())
if self.xnum.get()!=0:
self.ynum.set(display)
else:
self.xnum.set(display)
self.digitsvar.set(txt)
def press_inverse(self, display):
pass
def press_equals(self,display):
if self.xnum.get() != 0:
self.ynum.set(display)
else:
self.xnum.set(display)
x=self.xnum.get()
y=self.ynum.get()
op=self.operator.get()
if all([x,y,op]):
result=eval(f'{x}{op}{y}')
self.digitsvar.set(result)
self.reset_variables()
if __name__=='__main__':
app=ttk.Window(
title='Calculator',
themename='flatly',
size=(350,430),
resizable=(False,False)
)
app.wm_attributes('-topmost',1)#始终放在最前面
Calculator(app)
app.mainloop()
tkinter 计算器案例
最新推荐文章于 2024-01-24 16:08:13 发布