python制作简单计算器功能
程序员文章站
2022-06-15 14:36:59
本文实例为大家分享了python实现简单计算器功能的具体代码,供大家参考,具体内容如下效果如图:主要思路:用列表保存按下的键,按下等于,转换为字符串,利用内置函数eval计算字符串的值。代码:from...
本文实例为大家分享了python实现简单计算器功能的具体代码,供大家参考,具体内容如下
效果如图:
主要思路:
用列表保存按下的键,按下等于,转换为字符串,利用内置函数eval计算字符串的值。
代码:
from tkinter import * w = 280 #窗口宽度 h = 460 #窗口高度 process_h = 110 #显示运算过程的标签高度 result_h = 50 #显示运算结果的标签高度 msfont = '微软雅黑' #字体 fontsize = 20 #字体大小 btnboderwidth = 0.5 #边框宽度 btncolor = '#4f4f4f' #按钮颜色 btnwidth = 70 #按钮宽度 btnheight = 60 #按钮高度 mainwindows = tk() mainwindows.title('计算器') mainwindows.minsize(w,h) str_process = stringvar() str_process.set("") str_result = stringvar() str_result.set("0") process = label(mainwindows,font=(msfont,fontsize),bg='orange',anchor='se',wraplength='280',textvariable=str_process) process.place(width=w,height=process_h) #显示运算过程的标签 result = label(mainwindows,font=(msfont,fontsize+10),bg='orange',anchor='se',textvariable=str_result) result.place(y=process_h,width=w,height=result_h) #显示运算结果的标签 button_ac = button(mainwindows,font=(msfont,fontsize),text='ac',fg='orange',bd=btnboderwidth,command=lambda :clickac()) button_ac.place(x=0,y=process_h+result_h,width=btnwidth,height=btnheight) button_back = button(mainwindows,font=(msfont,fontsize),text='←',fg=btncolor,bd=btnboderwidth,command=lambda :clickback()) button_back.place(x=btnwidth,y=process_h+result_h,width=btnwidth,height=btnheight) button_div = button(mainwindows,font=(msfont,fontsize),text='÷',fg=btncolor,bd=btnboderwidth,command=lambda :clickoper('/')) button_div.place(x=btnwidth*2,y=process_h+result_h,width=btnwidth,height=btnheight) button_mul = button(mainwindows,font=(msfont,fontsize),text='×',fg=btncolor,bd=btnboderwidth,command=lambda :clickoper('*')) button_mul.place(x=btnwidth*3,y=process_h+result_h,width=btnwidth,height=btnheight) button_7 = button(mainwindows,font=(msfont,fontsize),text='7',fg=btncolor,bd=btnboderwidth,command=lambda :clicknum('7')) button_7.place(x=0,y=process_h+result_h+btnheight,width=btnwidth,height=btnheight) button_8 = button(mainwindows,font=(msfont,fontsize),text='8',fg=btncolor,bd=btnboderwidth,command=lambda :clicknum('8')) button_8.place(x=btnwidth,y=process_h+result_h+btnheight,width=btnwidth,height=btnheight) button_9 = button(mainwindows,font=(msfont,fontsize),text='9',fg=btncolor,bd=btnboderwidth,command=lambda :clicknum('9')) button_9.place(x=btnwidth*2,y=process_h+result_h+btnheight,width=btnwidth,height=btnheight) button_minus = button(mainwindows,font=(msfont,fontsize),text='-',fg=btncolor,bd=btnboderwidth,command=lambda :clickoper('-')) button_minus.place(x=btnwidth*3,y=process_h+result_h+btnheight,width=btnwidth,height=btnheight) button_4 = button(mainwindows,font=(msfont,fontsize),text='4',fg=btncolor,bd=btnboderwidth,command=lambda :clicknum('4')) button_4.place(x=0,y=process_h+result_h+btnheight*2,width=btnwidth,height=btnheight) button_5 = button(mainwindows,font=(msfont,fontsize),text='5',fg=btncolor,bd=btnboderwidth,command=lambda :clicknum('5')) button_5.place(x=btnwidth,y=process_h+result_h+btnheight*2,width=btnwidth,height=btnheight) button_6 = button(mainwindows,font=(msfont,fontsize),text='6',fg=btncolor,bd=btnboderwidth,command=lambda :clicknum('6')) button_6.place(x=btnwidth*2,y=process_h+result_h+btnheight*2,width=btnwidth,height=btnheight) button_add = button(mainwindows,font=(msfont,fontsize),text='+',fg=btncolor,bd=btnboderwidth,command=lambda :clickoper('+')) button_add.place(x=btnwidth*3,y=process_h+result_h+btnheight*2,width=btnwidth,height=btnheight) button_1 = button(mainwindows,font=(msfont,fontsize),text='1',fg=btncolor,bd=btnboderwidth,command=lambda :clicknum('1')) button_1.place(x=0,y=process_h+result_h+btnheight*3,width=btnwidth,height=btnheight) button_2 = button(mainwindows,font=(msfont,fontsize),text='2',fg=btncolor,bd=btnboderwidth,command=lambda :clicknum('2')) button_2.place(x=btnwidth,y=process_h+result_h+btnheight*3,width=btnwidth,height=btnheight) button_3 = button(mainwindows,font=(msfont,fontsize),text='3',fg=btncolor,bd=btnboderwidth,command=lambda :clicknum('3')) button_3.place(x=btnwidth*2,y=process_h+result_h+btnheight*3,width=btnwidth,height=btnheight) button_equal = button(mainwindows,font=(msfont,fontsize),text='=',bg='orange',fg=btncolor,bd=btnboderwidth,command=lambda :clickequal()) button_equal.place(x=btnwidth*3,y=process_h+result_h+btnheight*3,width=btnwidth,height=btnheight*2) button_percent = button(mainwindows,font=(msfont,fontsize),text='%',fg=btncolor,bd=btnboderwidth,command=lambda :clickoper("%")) button_percent.place(x=0,y=process_h+result_h+btnheight*4,width=btnwidth,height=btnheight) button_0 = button(mainwindows,font=(msfont,fontsize),text='0',fg=btncolor,bd=btnboderwidth,command=lambda :clicknum('0')) button_0.place(x=btnwidth,y=process_h+result_h+btnheight*4,width=btnwidth,height=btnheight) button_point = button(mainwindows,font=(msfont,fontsize),text='.',fg=btncolor,bd=btnboderwidth,command=lambda :clickpoint()) button_point.place(x=btnwidth*2,y=process_h+result_h+btnheight*4,width=btnwidth,height=btnheight) process_list = [] s_result = "" isnum = [false] #上一位按下的是否是数字 point = [true] #小数点使用情况 def clicknum(num): #按下数字 isnum.append(true) point.append(point[-1]) #按下数字,小数点的标志不变 process_list.append(num) s_process = "".join(process_list) str_process.set(s_process) def clickoper(sign):#按下运算符 global isnum,point if isnum[-1]: process_list.append(sign) isnum.append(false) point.append(true) #按下运算符,小数点标志为可以按下小数点 else: process_list.pop() process_list.append(sign) s_process = "".join(process_list) str_process.set(s_process) def clickequal():#按下等于 global s_result s_process = "".join(process_list) s_result = eval(s_process) s_result = str(s_result)[0:11] #结果只显示11位 str_process.set(s_process) str_result.set(s_result) def clickac():#按下清除 global s_result,isnum,point s_result = "0" isnum = [false] #状态回到初始时候 point = [true] process_list.clear() str_result.set(s_result) str_process.set("") def clickback(): #按下退格键 global point, isnum if len(process_list) > 0: isnum.pop(-1) #删除最后一位的状态 point.pop(-1) process_list.pop() s_process = "".join(process_list) str_process.set(s_process) def clickpoint():#按下小数点 global point,isnum if isnum[-1] and point[-1]: process_list.append(".") s_process = "".join(process_list) str_process.set(s_process) isnum.append(false) point.append(false) mainwindows.mainloop()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 草船借箭变数颇多,为何最后还能成功呢?
下一篇: 【C#编程最佳实践 七】代码书写规范实践