博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python小实例——tkinter实战(计算器)
阅读量:4545 次
发布时间:2019-06-08

本文共 12044 字,大约阅读时间需要 40 分钟。

 

一、完美计算器实验一

 

1 import tkinter  2 import math  3 import tkinter.messagebox  4    5 class calculator:  6     #界面布局方法  7     def __init__(self):  8         #创建主界面,并且保存到成员属性中  9         self.root = tkinter.Tk() 10         self.root.minsize(280, 450) 11         self.root.maxsize(280, 470) 12         self.root.title('小餅餅丶的简易计算器1.0') 13         # 设置显式面板的变量 14         self.result = tkinter.StringVar() 15         self.result.set(0) 16         # 设置一个全局变量  运算数字和f符号的列表 17         self.lists = [] 18         # 添加一个用于判断是否按下运算符号的标志 19         self.ispresssign = False 20         # 界面布局 21         self.menus() 22         self.layout() 23         self.root.mainloop() 24   25   26     #计算器菜单界面摆放 27     def menus(self): 28         # 添加菜单 29         # 创建总菜单 30         allmenu = tkinter.Menu(self.root) 31         # 添加子菜单 32         filemenu = tkinter.Menu(allmenu, tearoff=0) 33         # 添加选项卡 34         filemenu.add_command(label='标准型(T)            Alt+1', command=self.myfunc) 35         filemenu.add_command(label='科学型(S)            Alt+2', command=self.myfunc) 36         filemenu.add_command(label='程序员(P)            Alt+3', command=self.myfunc) 37         filemenu.add_command(label='统计信息(A)        Alt+4', command=self.myfunc) 38         # 添加分割线 39         filemenu.add_separator() 40         # 添加选项卡 41         filemenu.add_command(label='历史记录(Y)      Ctrl+H', command=self.myfunc) 42         filemenu.add_command(label='数字分组(I)', command=self.myfunc) 43         # 添加分割线 44         filemenu.add_separator() 45         # 添加选项卡 46         filemenu.add_command(label='基本(B)             Ctrl+F4', command=self.myfunc) 47         filemenu.add_command(label='单位转换(U)      Ctrl+U', command=self.myfunc) 48         filemenu.add_command(label='日期计算(D)      Ctrl+E', command=self.myfunc) 49         menu1 = tkinter.Menu(filemenu, tearoff=0) 50         menu1.add_command(label='抵押(M)', command=self.myfunc) 51         menu1.add_command(label='汽车租赁(V)', command=self.myfunc) 52         menu1.add_command(label='油耗(mpg)(F)', command=self.myfunc) 53         menu1.add_command(label='油耗(l/100km)(U)', command=self.myfunc) 54         filemenu.add_cascade(label='工作表(W)', menu=menu1) 55         allmenu.add_cascade(label='查看(V)', menu=filemenu) 56   57         # 添加子菜单2 58         editmenu = tkinter.Menu(allmenu, tearoff=0) 59         # 添加选项卡 60         editmenu.add_command(label='复制(C)         Ctrl+C', command=self.myfunc) 61         editmenu.add_command(label='粘贴(V)         Ctrl+V', command=self.myfunc) 62         # 添加分割线 63         editmenu.add_separator() 64         # 添加选项卡 65         menu2 = tkinter.Menu(filemenu, tearoff=0) 66         menu2.add_command(label='复制历史记录(I)', command=self.myfunc) 67         menu2.add_command(label='编辑(E)                      F2', command=self.myfunc) 68         menu2.add_command(label='取消编辑(N)            Esc', command=self.myfunc) 69         menu2.add_command(label='清除(L)    Ctrl+Shift+D', command=self.myfunc) 70         editmenu.add_cascade(label='历史记录(H)', menu=menu2) 71         allmenu.add_cascade(label='编辑(E)', menu=editmenu) 72   73         # 添加子菜单3 74         helpmenu = tkinter.Menu(allmenu, tearoff=0) 75         # 添加选项卡 76         helpmenu.add_command(label='查看帮助(V)       F1', command=self.myfunc) 77         # 添加分割线 78         helpmenu.add_separator() 79         # 添加选项卡 80         helpmenu.add_command(label='关于计算器(A)', command=self.myfunc) 81         allmenu.add_cascade(label='帮助(H)', menu=helpmenu) 82   83         self.root.config(menu=allmenu) 84   85   86     #计算器主界面摆放 87     def layout(self): 88         # 显示屏 89         result = tkinter.StringVar() 90         result.set(0) 91         show_label = tkinter.Label(self.root, bd=3, bg='white', font=('宋体', 30), anchor='e', textvariable=self.result) 92         show_label.place(x=5, y=20, width=270, height=70) 93         # 功能按钮MC 94         button_mc = tkinter.Button(self.root, text='MC', command=self.wait) 95         button_mc.place(x=5, y=95, width=50, height=50) 96         # 功能按钮MR 97         button_mr = tkinter.Button(self.root, text='MR', command=self.wait) 98         button_mr.place(x=60, y=95, width=50, height=50) 99         # 功能按钮MS100         button_ms = tkinter.Button(self.root, text='MS', command=self.wait)101         button_ms.place(x=115, y=95, width=50, height=50)102         # 功能按钮M+103         button_mjia = tkinter.Button(self.root, text='M+', command=self.wait)104         button_mjia.place(x=170, y=95, width=50, height=50)105         # 功能按钮M-106         button_mjian = tkinter.Button(self.root, text='M-', command=self.wait)107         button_mjian.place(x=225, y=95, width=50, height=50)108         # 功能按钮←109         button_zuo = tkinter.Button(self.root, text='←', command=self.dele_one)110         button_zuo.place(x=5, y=150, width=50, height=50)111         # 功能按钮CE112         button_ce = tkinter.Button(self.root, text='CE', command=lambda: self.result.set(0))113         button_ce.place(x=60, y=150, width=50, height=50)114         # 功能按钮C115         button_c = tkinter.Button(self.root, text='C', command=self.sweeppress)116         button_c.place(x=115, y=150, width=50, height=50)117         # 功能按钮±118         button_zf = tkinter.Button(self.root, text='±', command=self.zf)119         button_zf.place(x=170, y=150, width=50, height=50)120         # 功能按钮√121         button_kpf = tkinter.Button(self.root, text='√', command=self.kpf)122         button_kpf.place(x=225, y=150, width=50, height=50)123         # 数字按钮7124         button_7 = tkinter.Button(self.root, text='7', command=lambda: self.pressnum('7'))125         button_7.place(x=5, y=205, width=50, height=50)126         # 数字按钮8127         button_8 = tkinter.Button(self.root, text='8', command=lambda: self.pressnum('8'))128         button_8.place(x=60, y=205, width=50, height=50)129         # 数字按钮9130         button_9 = tkinter.Button(self.root, text='9', command=lambda: self.pressnum('9'))131         button_9.place(x=115, y=205, width=50, height=50)132         # 功能按钮/133         button_division = tkinter.Button(self.root, text='/', command=lambda: self.presscalculate('/'))134         button_division.place(x=170, y=205, width=50, height=50)135         # 功能按钮%136         button_remainder = tkinter.Button(self.root, text='//', command=lambda:self.presscalculate('//'))137         button_remainder.place(x=225, y=205, width=50, height=50)138         # 数字按钮4139         button_4 = tkinter.Button(self.root, text='4', command=lambda: self.pressnum('4'))140         button_4.place(x=5, y=260, width=50, height=50)141         # 数字按钮5142         button_5 = tkinter.Button(self.root, text='5', command=lambda: self.pressnum('5'))143         button_5.place(x=60, y=260, width=50, height=50)144         # 数字按钮6145         button_6 = tkinter.Button(self.root, text='6', command=lambda: self.pressnum('6'))146         button_6.place(x=115, y=260, width=50, height=50)147         # 功能按钮*148         button_multiplication = tkinter.Button(self.root, text='*', command=lambda: self.presscalculate('*'))149         button_multiplication.place(x=170, y=260, width=50, height=50)150         # 功能按钮1/x151         button_reciprocal = tkinter.Button(self.root, text='1/x', command=self.ds)152         button_reciprocal.place(x=225, y=260, width=50, height=50)153         # 数字按钮1154         button_1 = tkinter.Button(self.root, text='1', command=lambda: self.pressnum('1'))155         button_1.place(x=5, y=315, width=50, height=50)156         # 数字按钮2157         button_2 = tkinter.Button(self.root, text='2', command=lambda: self.pressnum('2'))158         button_2.place(x=60, y=315, width=50, height=50)159         # 数字按钮3160         button_3 = tkinter.Button(self.root, text='3', command=lambda: self.pressnum('3'))161         button_3.place(x=115, y=315, width=50, height=50)162         # 功能按钮-163         button_subtraction = tkinter.Button(self.root, text='-', command=lambda: self.presscalculate('-'))164         button_subtraction.place(x=170, y=315, width=50, height=50)165         # 功能按钮=166         button_equal = tkinter.Button(self.root, text='=', command=lambda: self.pressequal())167         button_equal.place(x=225, y=315, width=50, height=105)168         # 数字按钮0169         button_0 = tkinter.Button(self.root, text='0', command=lambda: self.pressnum('0'))170         button_0.place(x=5, y=370, width=105, height=50)171         # 功能按钮.172         button_point = tkinter.Button(self.root, text='.', command=lambda: self.pressnum('.'))173         button_point.place(x=115, y=370, width=50, height=50)174         # 功能按钮+175         button_plus = tkinter.Button(self.root, text='+', command=lambda: self.presscalculate('+'))176         button_plus.place(x=170, y=370, width=50, height=50)177  178  179     #计算器菜单功能180     def myfunc(self):181         tkinter.messagebox.showinfo('','程序员懒死在电脑前,打死也做不出的功能,只是装饰而已~')182  183  184     #数字方法185     def pressnum(self,num):186         # 全局化变量187         # 判断是否按下了运算符号188         if self.ispresssign == False:189             pass190         else:191             self.result.set(0)192             # 重置运算符号的状态193             self.ispresssign = False194         if num == '.':195             num = '0.'196         # 获取面板中的原有数字197         oldnum = self.result.get()198         # 判断界面数字是否为0199         if oldnum == '0':200             self.result.set(num)201         else:202             # 连接上新按下的数字203             newnum = oldnum + num204  205             # 将按下的数字写到面板中206             self.result.set(newnum)207  208  209     #运算函数210     def presscalculate(self,sign):211         # 保存已经按下的数字和运算符号212         # 获取界面数字213         num = self.result.get()214         self.lists.append(num)215         # 保存按下的操作符号216         self.lists.append(sign)217         # 设置运算符号为按下状态218         self.ispresssign = True219  220  221     #获取运算结果222     def pressequal(self):223         # 获取所有的列表中的内容(之前的数字和操作)224         # 获取当前界面上的数字225         curnum = self.result.get()226         # 将当前界面的数字存入列表227         self.lists.append(curnum)228         # 将列表转化为字符串229         calculatestr = ''.join(self.lists)230         # 使用eval执行字符串中的运算即可231         endnum = eval(calculatestr)232         # 将运算结果显示在界面中233         self.result.set(str(endnum)[:10])234         if self.lists != 0:235             self.ispresssign = True236         # 清空运算列表237         self.lists.clear()238  239  240     #暂未开发说明241     def wait(self):242         tkinter.messagebox.showinfo('','功能在努力的实现,请期待2.0版本的更新')243  244  245     #←按键功能246     def dele_one(self):247         if self.result.get() == '' or self.result.get() == '0':248             self.result.set('0')249             return250         else:251             num = len(self.result.get())252             if num > 1:253                 strnum = self.result.get()254                 strnum = strnum[0:num - 1]255                 self.result.set(strnum)256             else:257                 self.result.set('0')258  259  260     #±按键功能261     def zf(self):262         strnum = self.result.get()263         if strnum[0] == '-':264             self.result.set(strnum[1:])265         elif strnum[0] != '-' and strnum != '0':266             self.result.set('-' + strnum)267  268  269     #1/x按键功能270     def ds(self):271         dsnum = 1 / int(self.result.get())272         self.result.set(str(dsnum)[:10])273         if self.lists != 0:274             self.ispresssign = True275         # 清空运算列表276         self.lists.clear()277  278  279     #C按键功能280     def sweeppress(self):281         self.lists.clear()282         self.result.set(0)283  284  285     #√按键功能286     def kpf(self):287         strnum = float(self.result.get())288         endnum = math.sqrt(strnum)289         if str(endnum)[-1] == '0':290             self.result.set(str(endnum)[:-2])291         else:292             self.result.set(str(endnum)[:10])293         if self.lists != 0:294             self.ispresssign = True295         # 清空运算列表296         self.lists.clear()297  298  299 #实例化对象300 mycalculator = calculator()

来源于https://www.cnblogs.com/xiaobingbing/p/8016997.html

转载于:https://www.cnblogs.com/xxtalhr/p/11089523.html

你可能感兴趣的文章
【Codeforces Round #455 (Div. 2) A】Generate Login
查看>>
【Codeforces 476C】Dreamoon and Sums
查看>>
【codeforces 505C】Mr.Kitayuta,the Treasure Hunter
查看>>
混合应用 微信登录授权 微信登录认证失败 ios PGWXAPI错误-1 code:-100 / 安卓 message:invalid appsecret innerCode:40125...
查看>>
MSSQL按分页前去盘查了局的存储进程
查看>>
运用centosplus来更新你的CentOS
查看>>
wxDownload Fast 0.4.5
查看>>
<实训|第十天>从底层解释一下U盘内存为什么变小的原因附数据恢复的基本原理...
查看>>
测试Windows Live Writer插件
查看>>
常用正则封装,积少成多
查看>>
文件下载工具类
查看>>
Python 定义自己的常量类
查看>>
C++读取文本文件
查看>>
Python 字典排序
查看>>
sql中写标量函数生成大写拼音首字母
查看>>
ASP.NET Core 2.1 : 十五.图解路由(2.1 or earler)
查看>>
服务器返回状态码说明
查看>>
判断浏览器内核(转)
查看>>
GitHub for Windows提交失败“failed to sync this branch”
查看>>
linux 安装 git
查看>>