基于python+tkinter的计算器练习
文章目录
- 设计原理
- 示例效果
设计原理
从结构上来说,一个简单的图形界面,需要由界面组件、组件的事件监听器(响应各类事件的逻辑)和具体的事件处理逻辑组成。界面实现的主要工作是创建各个界面组件对象,对其进行初始化,以及控制各组件之间的层次关系和布局
示例效果
import tkinter
import math
import tkinter.messageboxclass Calculator(object):# 界面布局方法def __init__(self):# 创建主界面,并且保存到成员属性中self.root = tkinter.Tk()self.root.minsize(280, 450)self.root.maxsize(280, 470)self.root.title('计算器')# 设置显式面板的变量self.result = tkinter.StringVar()self.result.set(0)# 设置一个全局变量 运算数字和f符号的列表self.lists = []# 添加一个用于判断是否按下运算符号的标志self.ispresssign = False# 界面布局self.menus()self.layout()self.root.mainloop()# 计算器菜单界面摆放def menus(self):# 添加菜单# 创建总菜单allmenu = tkinter.Menu(self.root)# 添加子菜单filemenu = tkinter.Menu(allmenu, tearoff=0)# 添加选项卡filemenu.add_command(label='标准型(T) Alt+1', command=self.myfunc)filemenu.add_command(label='科学型(S) Alt+2', command=self.myfunc)filemenu.add_command(label='程序员(P) Alt+3', command=self.myfunc)filemenu.add_command(label='统计信息(A) Alt+4', command=self.myfunc)# 添加分割线filemenu.add_separator()# 添加选项卡filemenu.add_command(label='历史记录(Y) Ctrl+H', command=self.myfunc)filemenu.add_command(label='数字分组(I)', command=self.myfunc)# 添加分割线filemenu.add_separator()# 添加选项卡filemenu.add_command(label='基本(B) Ctrl+F4', command=self.myfunc)filemenu.add_command(label='单位转换(U) Ctrl+U', command=self.myfunc)filemenu.add_command(label='日期计算(D) Ctrl+E', command=self.myfunc)menu1 = tkinter.Menu(filemenu, tearoff=0)menu1.add_command(label='抵押(M)', command=self.myfunc)menu1.add_command(label='汽车租赁(V)', command=self.myfunc)menu1.add_command(label='油耗(mpg)(F)', command=self.myfunc)menu1.add_command(label='油耗(l/100km)(U)', command=self.myfunc)filemenu.add_cascade(label='工作表(W)', menu=menu1)allmenu.add_cascade(label='查看(V)', menu=filemenu)# 添加子菜单2editmenu = tkinter.Menu(allmenu, tearoff=0)# 添加选项卡editmenu.add_command(label='复制(C) Ctrl+C', command=self.myfunc)editmenu.add_command(label='粘贴(V) Ctrl+V', command=self.myfunc)# 添加分割线editmenu.add_separator()# 添加选项卡menu2 = tkinter.Menu(filemenu, tearoff=0)menu2.add_command(label='复制历史记录(I)', command=self.myfunc)menu2.add_command(label='编辑(E) F2', command=self.myfunc)menu2.add_command(label='取消编辑(N) Esc', command=self.myfunc)menu2.add_command(label='清除(L) Ctrl+Shift+D', command=self.myfunc)editmenu.add_cascade(label='历史记录(H)', menu=menu2)allmenu.add_cascade(label='编辑(E)', menu=editmenu)# 添加子菜单3helpmenu = tkinter.Menu(allmenu, tearoff=0)# 添加选项卡helpmenu.add_command(label='查看帮助(V) F1', command=self.myfunc)# 添加分割线helpmenu.add_separator()# 添加选项卡helpmenu.add_command(label='关于计算器(A)', command=self.myfunc)allmenu.add_cascade(label='帮助(H)', menu=helpmenu)self.root.config(menu=allmenu)# 计算器主界面摆放def layout(self):# 显示屏result = tkinter.StringVar()result.set(0)show_label = tkinter.Label(self.root, bd=3, bg='white', font=('宋体', 30), anchor='e', textvariable=self.result)show_label.place(x=5, y=20, width=270, height=70)# 功能按钮MCbutton_mc = tkinter.Button(self.root, text='MC', command=self.wait)button_mc.place(x=5, y=95, width=50, height=50)# 功能按钮MRbutton_mr = tkinter.Button(self.root, text='MR', command=self.wait)button_mr.place(x=60, y=95, width=50, height=50)# 功能按钮MSbutton_ms = tkinter.Button(self.root, text='MS', command=self.wait)button_ms.place(x=115, y=95, width=50, height=50)# 功能按钮M+button_mjia = tkinter.Button(self.root, text='M+', command=self.wait)button_mjia.place(x=170, y=95, width=50, height=50)# 功能按钮M-button_mjian = tkinter.Button(self.root, text='M-', command=self.wait)button_mjian.place(x=225, y=95, width=50, height=50)# 功能按钮←button_zuo = tkinter.Button(self.root, text='←', command=self.dele_one)button_zuo.place(x=5, y=150, width=50, height=50)# 功能按钮CEbutton_ce = tkinter.Button(self.root, text='CE', command=lambda: self.result.set(0))button_ce.place(x=60, y=150, width=50, height=50)# 功能按钮Cbutton_c = tkinter.Button(self.root, text='C', command=self.sweeppress)button_c.place(x=115, y=150, width=50, height=50)# 功能按钮±button_zf = tkinter.Button(self.root, text='±', command=self.zf)button_zf.place(x=170, y=150, width=50, height=50)# 功能按钮√button_kpf = tkinter.Button(self.root, text='√', command=self.kpf)button_kpf.place(x=225, y=150, width=50, height=50)# 数字按钮7button_7 = tkinter.Button(self.root, text='7', command=lambda: self.pressnum('7'))button_7.place(x=5, y=205, width=50, height=50)# 数字按钮8button_8 = tkinter.Button(self.root, text='8', command=lambda: self.pressnum('8'))button_8.place(x=60, y=205, width=50, height=50)# 数字按钮9button_9 = tkinter.Button(self.root, text='9', command=lambda: self.pressnum('9'))button_9.place(x=115, y=205, width=50, height=50)# 功能按钮/button_division = tkinter.Button(self.root, text='/', command=lambda: self.presscalculate('/'))button_division.place(x=170, y=205, width=50, height=50)# 功能按钮%button_remainder = tkinter.Button(self.root, text='//', command=lambda: self.presscalculate('//'))button_remainder.place(x=225, y=205, width=50, height=50)# 数字按钮4button_4 = tkinter.Button(self.root, text='4', command=lambda: self.pressnum('4'))button_4.place(x=5, y=260, width=50, height=50)# 数字按钮5button_5 = tkinter.Button(self.root, text='5', command=lambda: self.pressnum('5'))button_5.place(x=60, y=260, width=50, height=50)# 数字按钮6button_6 = tkinter.Button(self.root, text='6', command=lambda: self.pressnum('6'))button_6.place(x=115, y=260, width=50, height=50)# 功能按钮*button_multiplication = tkinter.Button(self.root, text='*', command=lambda: self.presscalculate('*'))button_multiplication.place(x=170, y=260, width=50, height=50)# 功能按钮1/xbutton_reciprocal = tkinter.Button(self.root, text='1/x', command=self.ds)button_reciprocal.place(x=225, y=260, width=50, height=50)# 数字按钮1button_1 = tkinter.Button(self.root, text='1', command=lambda: self.pressnum('1'))button_1.place(x=5, y=315, width=50, height=50)# 数字按钮2button_2 = tkinter.Button(self.root, text='2', command=lambda: self.pressnum('2'))button_2.place(x=60, y=315, width=50, height=50)# 数字按钮3button_3 = tkinter.Button(self.root, text='3', command=lambda: self.pressnum('3'))button_3.place(x=115, y=315, width=50, height=50)# 功能按钮-button_subtraction = tkinter.Button(self.root, text='-', command=lambda: self.presscalculate('-'))button_subtraction.place(x=170, y=315, width=50, height=50)# 功能按钮=button_equal = tkinter.Button(self.root, text='=', command=lambda: self.pressequal())button_equal.place(x=225, y=315, width=50, height=105)# 数字按钮0button_0 = tkinter.Button(self.root, text='0', command=lambda: self.pressnum('0'))button_0.place(x=5, y=370, width=105, height=50)# 功能按钮.button_point = tkinter.Button(self.root, text='.', command=lambda: self.pressnum('.'))button_point.place(x=115, y=370, width=50, height=50)# 功能按钮+button_plus = tkinter.Button(self.root, text='+', command=lambda: self.presscalculate('+'))button_plus.place(x=170, y=370, width=50, height=50)# 计算器菜单功能def myfunc(self):tkinter.messagebox.showinfo('', '预留接口,学成之后,你是不是有冲动添加该功能.')# 数字方法def pressnum(self, num):# 全局化变量# 判断是否按下了运算符号if self.ispresssign == False:passelse:self.result.set(0)# 重置运算符号的状态self.ispresssign = Falseif num == '.':num = '0.'# 获取面板中的原有数字oldnum = self.result.get()# 判断界面数字是否为0if oldnum == '0':self.result.set(num)else:# 连接上新按下的数字newnum = oldnum + num# 将按下的数字写到面板中self.result.set(newnum)# 运算函数def presscalculate(self, sign):# 保存已经按下的数字和运算符号# 获取界面数字num = self.result.get()self.lists.append(num)# 保存按下的操作符号self.lists.append(sign)# 设置运算符号为按下状态self.ispresssign = True# 获取运算结果def pressequal(self):# 获取所有的列表中的内容(之前的数字和操作)# 获取当前界面上的数字curnum = self.result.get()# 将当前界面的数字存入列表self.lists.append(curnum)# 将列表转化为字符串calculatestr = ''.join(self.lists)# 使用eval执行字符串中的运算即可endnum = eval(calculatestr)# 将运算结果显示在界面中self.result.set(str(endnum)[:10])if self.lists != 0:self.ispresssign = True# 清空运算列表self.lists.clear()# 暂未开发说明def wait(self):tkinter.messagebox.showinfo('', '更新中......')# ←按键功能def dele_one(self):if self.result.get() == '' or self.result.get() == '0':self.result.set('0')returnelse:num = len(self.result.get())if num > 1:strnum = self.result.get()strnum = strnum[0:num - 1]self.result.set(strnum)else:self.result.set('0')# ±按键功能def zf(self):strnum = self.result.get()if strnum[0] == '-':self.result.set(strnum[1:])elif strnum[0] != '-' and strnum != '0':self.result.set('-' + strnum)# 1/x按键功能def ds(self):dsnum = 1 / int(self.result.get())self.result.set(str(dsnum)[:10])if self.lists != 0:self.ispresssign = True# 清空运算列表self.lists.clear()# C按键功能def sweeppress(self):self.lists.clear()self.result.set(0)# √按键功能def kpf(self):strnum = float(self.result.get())endnum = math.sqrt(strnum)if str(endnum)[-1] == '0':self.result.set(str(endnum)[:-2])else:self.result.set(str(endnum)[:10])if self.lists != 0:self.ispresssign = True# 清空运算列表self.lists.clear()# 实例化对象
my_calculator = Calculator()
基于python+tkinter的计算器练习相关推荐
- python点名代码_基于python tkinter的点名小程序功能的实例代码
基于python tkinter的点名小程序功能的实例代码,花名册,次数,窗口,未找到,初始化 基于python tkinter的点名小程序功能的实例代码 易采站长站,站长之家为您整理了基于pytho ...
- 【基于Python+tkinter的音乐播放器开发-哔哩哔哩】 https://b23.tv/eG2TwOL
[基于Python+tkinter的音乐播放器开发-哔哩哔哩] https://b23.tv/eG2TwOL https://b23.tv/eG2TwOL
- 基于Python/Tkinter的RGB颜色查询器
这是基于Python/Tkinter的16位RGB颜色查询器,因为之前在做WEB前端设计配色方案的时候,需要了解色值,每次用网页查询颜色对应的RGB值和HEX值,很不方便,所以就想写个单机程序,原本是 ...
- 基于Python/Tkinter的飞机大战单机小游戏
这是很早之前课余时间写的基于Python/Tkinter单机小游戏,用来练手,今天将代码贴出来,方便大家一起学习,通过Py/Tk对于学习GUI作为一个入口,其实是个不错入口,在这里推荐一下Tcl/Tk ...
- 基于python+tkinter的学生成绩信息管理系统
基于python+tkinter的学生成绩信息管理系统 系统设计 2.开发工具 开发语言:python3.6.8 开发工具:JetBrains PyCharm 2019.1.2 x64 使用三方模块: ...
- 基于python tkinter的简单计算器(v1.0)
import tkinter#定义计算器类 class Calc:#初始化魔术方法def __init__(self):#初始化共用属性#定义一个用于存放被计算字符串的列表self.operation ...
- 基于Python Tkinter的多线程局域网扫描器
#本文仅供参考有不足之处请指出 一.设计环境 系统:Windows 11 语言:python3.8 编译器:PyCharm 二.设计要求 1.局域网内的存活主机发现 2.扫描指定主机开放的端口 3.图 ...
- Python tkinter实现计算器
python版本:3.5 一.计算器的功能描述 今天我们用python来实现一个计算器.首先,计算器分为两部分:一部分是初级页面,包括简单的加减乘除四则运算.第二部分:是高级页面,包括常见的三角函数. ...
- 基于Python+Tkinter GUI 的模式识别水果分类小程序
采用Python语言编写,并结合Tkinter GUI工具制作交互式小程序开发,实现了简单的水果的边缘提取和分类.如图1-A,用户可以自定义选择路径并输出,同时可以在对话框中输入/输出结果,如图1-B ...
最新文章
- php 缓存模块,PHP缓存之模块缓存(APC)_PHP教程
- google python代码规范_如何用好python编码规范,写一手漂亮的代码
- [转]WinXP、Win7脚本自动加域及用户资料迁移
- 笔记本电脑处理器_英特尔发布第九代酷睿移动处理器:笔记本电脑进入8核5.0GHz时代!...
- java怎么获取文本里的值_怎么获取到text中的文本,或者title中的值
- 江苏关于领取软考2021年上半年合格证书的通知
- spark.mllib:bagging方法
- Angular中父子组件传值@Input @Output @ViewChild最全面最简单的总结
- 亚马逊全面发力AI,推机器学习托管服务、四项新工具,还有AI硬件
- POJ1214 UVA127 Accordian Patience【vector】
- Sublime Text 3.1 编辑管理工程(项目)
- 解析WAP技术(转)
- mac上谷歌浏览器添加插件显示程序包无效的解决办法
- android图标包怎么安装,图标包怎么用 安卓好看的图标包推荐
- 基于openstack的云桌面开源框架
- 2023年深圳Java培训机构排名,不看后悔系列!
- Windows10设置挂起(休眠)
- echarts3.0 markline 最大值 最小值 均值 方差 标准差 包络 正态分布
- matlab:人脸识别
- [rtsp @ 0x55ba1dae9200] UDP timeout, retrying with TCP的解决办法
热门文章
- 秀杰音乐盒之mtv播放器 v1.0 怎么用
- 联想a668t各种root方法汇集
- 8bit 1GS/s 高速数据采集卡超宽带高速记录回放系统的分类和使用方法
- python制作图片居中加文字
- 斯阔谷冬奥会首次使用计算机,第八届美国斯阔谷冬奥会_温哥华冬奥会_新浪体育_新浪网...
- 车联网设计(基于veins+omnetpp+sumo)
- ThinkPad驱动下载
- python之类的静态方法
- jQuery 分页器
- 抖音 App 登录账号、密码、验证码 XOR 加密算法