基于python tkinter的简单计算器(v1.0)
发表于:2024-10-27 作者:千家信息网编辑
千家信息网最后更新 2024年10月27日,import tkinter#定义计算器类class Calc: #初始化魔术方法 def __init__(self): #初始化共用属性
千家信息网最后更新 2024年10月27日基于python tkinter的简单计算器(v1.0)
import tkinter#定义计算器类class Calc: #初始化魔术方法 def __init__(self): #初始化共用属性 #定义一个用于存放被计算字符串的列表 self.operationList = [] #定义运算标记 确定是否输入了运算符号 self.isOper = False #初始化界面 self.initWindows() #更改按键盘颜色方法 def changeBg(self,evt): evt.widget['bg'] = 'cyan' #恢复按键盘颜色方法 def backBg(self,evt): evt.widget['bg'] = 'lightgray' #数字按钮操作方法 def buttonAction(self,number): #判断用户是否按下了运算按钮 if self.isOper == True: #在界面上显示运算符之后的数 self.num.set(number) #运算标记复位 self.isOper = False else: #没有铵下运算按钮 #判断原始界面数字是否为0 existNumber = self.num.get() if existNumber == '0': #如果界面中的初始数据为0 则获取用户输入数据并显示 self.num.set(number) else: #如果界面中的初始数据不为0 则对字符进行累加 self.num.set(self.num.get()+number) #运算按钮操作方法 def operation(self,opFlag): #运算标记置为真 self.isOper = True #获取界面中存在的数 并且写入列表 self.operationList.append(self.num.get()) #当前运算符号不会在上一步中写入 需要单独写入 self.operationList.append(opFlag) #获取运行结果操作方法 def getResult(self): #将当前界面中数字加入计算列表 self.operationList.append(self.num.get()) #开始计算 result = eval(''.join(self.operationList)) self.num.set(result) #全部清空重新计算方法 def clearAll(self): #界面置0 计算列表置0 self.num.set('0') self.operationList.clear() #运算标志复位 self.isOper = False #实现退格键方法 def backSpace(self): #获取当前显示数字长度 strLength = len(self.num.get()) #如果当前显示有数字 if strLength > 1: #删除字串中最后一个字 presentStr = self.num.get() presentStr = presentStr[:strLength - 1] self.num.set(presentStr) else: self.num.set('0') #正负号实现方法 def pm(self): presentStr = self.num.get() #实现增加和去除负号 if presentStr[0] == '-': self.num.set(presentStr[1:]) #原始字串不得以-号和0开头 elif presentStr[0] not in ('-','0'): self.num.set('-'+presentStr) #界面布局方法 def initWindows(self): #生成主窗口 定制窗口尺寸 root = tkinter.Tk() root.minsize(400,500) root.title('微硬计算器') #生成用于保存数值的变量 self.num = tkinter.StringVar() self.num.set(0) #运算结果输出位置 result = tkinter.Label(root,width=20,height=2,bg='white',bd=10,anchor='e',font=('宋体',50),textvariable=self.num) result.place(relx=0,rely=0,relwidth=1.0,relheight=0.4) ###########################以下为按键部分############################ buttonCE = tkinter.Button(root,text='CE',bg='lightgray',command = self.clearAll) buttonCE.place(relx=0,rely=0.4,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 buttonCE.bind('',self.changeBg) buttonCE.bind(' ',self.backBg) buttonC = tkinter.Button(root,text='C',bg='lightgray',command = self.clearAll) buttonC.place(relx=0.25,rely=0.4,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 buttonC.bind(' ',self.changeBg) buttonC.bind(' ',self.backBg) buttonDel = tkinter.Button(root,text='<-',bg='lightgray',command = self.backSpace) buttonDel.place(relx=0.5,rely=0.4,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 buttonDel.bind(' ',self.changeBg) buttonDel.bind(' ',self.backBg) buttonDiv = tkinter.Button(root,text='÷',bg='lightgray',command = lambda : self.operation('/')) buttonDiv.place(relx=0.75,rely=0.4,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 buttonDiv.bind(' ',self.changeBg) buttonDiv.bind(' ',self.backBg) button1 = tkinter.Button(root,text='1',bg='lightgray',command = lambda : self.buttonAction('1')) button1.place(relx=0,rely=0.5,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 button1.bind(' ',self.changeBg) button1.bind(' ',self.backBg) button2 = tkinter.Button(root,text='2',bg='lightgray',command = lambda : self.buttonAction('2')) button2.place(relx=0.25,rely=0.5,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 button2.bind(' ',self.changeBg) button2.bind(' ',self.backBg) button3 = tkinter.Button(root,text='3',bg='lightgray',command = lambda : self.buttonAction('3')) button3.place(relx=0.5,rely=0.5,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 button3.bind(' ',self.changeBg) button3.bind(' ',self.backBg) buttonX = tkinter.Button(root,text='x',bg='lightgray',command = lambda : self.operation('*')) buttonX.place(relx=0.75,rely=0.5,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 buttonX.bind(' ',self.changeBg) buttonX.bind(' ',self.backBg) button4 = tkinter.Button(root,text='4',bg='lightgray',command = lambda : self.buttonAction('4')) button4.place(relx=0,rely=0.6,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 button4.bind(' ',self.changeBg) button4.bind(' ',self.backBg) button5 = tkinter.Button(root,text='5',bg='lightgray',command = lambda : self.buttonAction('5')) button5.place(relx=0.25,rely=0.6,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 button5.bind(' ',self.changeBg) button5.bind(' ',self.backBg) button6 = tkinter.Button(root,text='6',bg='lightgray',command = lambda : self.buttonAction('6')) button6.place(relx=0.5,rely=0.6,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 button6.bind(' ',self.changeBg) button6.bind(' ',self.backBg) button_ = tkinter.Button(root,text='-',bg='lightgray',command = lambda : self.operation('-')) button_.place(relx=0.75,rely=0.6,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 button_.bind(' ',self.changeBg) button_.bind(' ',self.backBg) button7 = tkinter.Button(root,text='7',bg='lightgray',command = lambda : self.buttonAction('7')) button7.place(relx=0,rely=0.7,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 button7.bind(' ',self.changeBg) button7.bind(' ',self.backBg) button8 = tkinter.Button(root,text='8',bg='lightgray',command = lambda : self.buttonAction('8')) button8.place(relx=0.25,rely=0.7,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 button8.bind(' ',self.changeBg) button8.bind(' ',self.backBg) button9 = tkinter.Button(root,text='9',bg='lightgray',command = lambda : self.buttonAction('9')) button9.place(relx=0.5,rely=0.7,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 button9.bind(' ',self.changeBg) button9.bind(' ',self.backBg) buttonAdd = tkinter.Button(root,text='+',bg='lightgray',command = lambda : self.operation('+')) buttonAdd.place(relx=0.75,rely=0.7,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 buttonAdd.bind(' ',self.changeBg) buttonAdd.bind(' ',self.backBg) buttonFlag = tkinter.Button(root,text='±',bg='lightgray',command = self.pm) buttonFlag.place(relx=0,rely=0.8,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 buttonFlag.bind(' ',self.changeBg) buttonFlag.bind(' ',self.backBg) button0 = tkinter.Button(root,text='0',bg='lightgray',command = lambda : self.buttonAction('0')) button0.place(relx=0.25,rely=0.8,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 button0.bind(' ',self.changeBg) button0.bind(' ',self.backBg) buttonPoint = tkinter.Button(root,text='.',bg='lightgray',command = lambda : self.buttonAction('.')) buttonPoint.place(relx=0.5,rely=0.8,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 buttonPoint.bind(' ',self.changeBg) buttonPoint.bind(' ',self.backBg) buttonEque = tkinter.Button(root,text='=',bg='lightgray',command = self.getResult) buttonEque.place(relx=0.75,rely=0.8,relwidth=0.25,relheight=0.1) #绑定按钮 生成鼠标经过变色效果 buttonEque.bind(' ',self.changeBg) buttonEque.bind(' ',self.backBg) #########################以上为按键部分############################ #底部显示信息 bottomLabel = tkinter.Label(root,text = 'Power By Microhard Corpration\n@2017' ,bg='cyan',width=30,height = 1,padx=0) bottomLabel.place(relx=0,rely=0.9,relwidth=1.0,relheight=0.1) #主窗口循环 root.mainloop() #实例化计算器对象c = Calc()
按钮
生成
效果
鼠标
变色
运算
方法
界面
数字
计算器
数据
标记
原始
字符
按键
用户
符号
结果
部分
键盘
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库中嵌套多表查询的规则
广州三丰互联网科技有限公司
软件开发毕业生简历范文
初中毕业做软件开发
学软件开发哪所学校
国外主流数据库管理系统简介
财务软件的后台数据库在
恐龙岛为什么服务器那么少
rpc服务器为什么进不了桌面
深州手机软件开发
扬州通信网络技术包括什么
查询数据库列长度限制
失落的方舟不在一个服务器
浙江软件开发服务电话
五华区品牌软件开发报价表
法条检索数据库设计
2022年春考网络技术
数据库表的自关联
六安网络安全考试公司
服务器内存插到台式电脑上
服务器电源转换效率怎么看
紫金山网络安全科技城
数据库界面操作方式
驾考理论培训软件开发公司
php 数据库连接 关闭
华为服务器光遇下载
rpc服务器为什么进不了桌面
mdb数据库怎么使用
php制作数据库教程学习
荣耀v30pro云服务器