#encoding=utf-8
import tkinter
import re
import tkinter.messagebox
import tkinter.simpledialog
import sys
import os
def get_resources_path(relative_path):if getattr(sys,'frozen', False):base_path=sys._MEIPASS#获取临时文件else:base_path=os.path.dirname(".")return os.path.join(base_path,relative_path)
Logopath=get_resources_path(os.path.join("resources",'heart.ico'))
startpath=get_resources_path(os.path.join("resources",'3.png'))
circlepath=get_resources_path(os.path.join("resources",'4.png'))
duopath=get_resources_path(os.path.join("resources",'5.png'))
class MainForm:#定义窗体类def __init__(self):self.root=tkinter.Tk()#创建一个窗体self.root.title("Katetesting")#设置标题self.root.iconbitmap(Logopath)self.root.geometry("231x280")self.root.maxsize(1000,1000)self.root["background"]="#312432"self.input_frame()self.button_frame()self.root.mainloop()#显示窗体def input_frame(self):self.input_frame=tkinter.Frame(self.root,width=20)self.content=tkinter.StringVar()#此时不是多行输入是单行输入,所以使用Entry组件self.entry=tkinter.Entry(self.input_frame,width=14,font=("微软雅黑",20),textvariable=self.content)self.entry.pack(fill="x",expand=1)self.clean=False#清除标记,每一次计算完成之后清除self.input_frame.pack(side="top")def button_frame(self):self.button_frame=tkinter.Frame(self.root,width=8)self.button_list=[[],[],[],[]]#定义了一个4组组件self.button_list[0].append(tkinter.Button(self.button_frame,text="1",fg="black",width=3,font=("微软雅黑",'20')))self.button_list[0].append(tkinter.Button(self.button_frame,text="2",fg="black",width=3,font=("微软雅黑",'20')))self.button_list[0].append(tkinter.Button(self.button_frame,text="3",fg="black",width=3,font=("微软雅黑",'20')))self.button_list[0].append(tkinter.Button(self.button_frame,text="+",fg="black",width=3,font=("微软雅黑",'20')))self.button_list[1].append(tkinter.Button(self.button_frame,text="4",fg="black",width=3,font=("微软雅黑",'20')))self.button_list[1].append(tkinter.Button(self.button_frame,text="5",fg="black",width=3,font=("微软雅黑",'20')))self.button_list[1].append(tkinter.Button(self.button_frame,text="6",fg="black",width=3,font=("微软雅黑",'20')))self.button_list[1].append(tkinter.Button(self.button_frame,text="-",fg="black",width=3,font=("微软雅黑",'20')))self.button_list[2].append(tkinter.Button(self.button_frame,text="7",fg="black",width=3,font=("微软雅黑",'20')))self.button_list[2].append(tkinter.Button(self.button_frame,text="8",fg="black",width=3,font=("微软雅黑",'20')))self.button_list[2].append(tkinter.Button(self.button_frame,text="9",fg="black",width=3,font=("微软雅黑",'20')))self.button_list[2].append(tkinter.Button(self.button_frame,text="*",fg="black",width=3,font=("微软雅黑",'20')))self.button_list[3].append(tkinter.Button(self.button_frame,text="0",fg="black",width=3,font=("微软雅黑",'20')))self.button_list[3].append(tkinter.Button(self.button_frame,text=".",fg="black",width=3,font=("微软雅黑",'20')))self.button_list[3].append(tkinter.Button(self.button_frame,text="=",fg="black",width=3,font=("微软雅黑",'20')))self.button_list[3].append(tkinter.Button(self.button_frame,text="/",fg="black",width=3,font=("微软雅黑",'20')))self.row=0#进行行数的控制for group in self.button_list:self.column=0#进行列的控制for button in group:button.bind("<Button-1>",lambda event:self.button_handle(event))#绑定事件button.grid(row=self.row,column=self.column)self.column+=1self.row+=1self.button_frame.pack(side="bottom")def button_handle(self,event):oper=event.widget["text"]if self.clean:#第二次计算self.content.set("")self.clean=False#留给下一次计算输入if oper!="=":self.entry.insert("end",oper)elif oper =="=":#执行运算result=0#保存程序的计算结果exp=self.entry.get()print(exp)pattern=r"\+|\-|\*|\\"nums=re.split(pattern,exp)print(nums)flag=re.findall(pattern,exp)[0]if flag=="+":result=float(nums[0])+float(nums[1])elif flag=="-":result = float(nums[0]) - float(nums[1])elif flag=="*":result = float(nums[0]) * float(nums[1])elif flag==r"/":result = float(nums[0]) / float(nums[1])self.entry.insert("end","=%s"%result)self.clean=Truedef main():MainForm()
if __name__=="__main__":main()
