浔川计算机v1.1——浔川python科技社

2024-06-15 13:52

本文主要是介绍浔川计算机v1.1——浔川python科技社,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

浔川计算机v1.1


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社、浔川python科技社联合创作

 浔川计算机v2.1 即将上线!(上线时间:7月2日)

这篇关于浔川计算机v1.1——浔川python科技社的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1063642

相关文章

Python 字典 (Dictionary)使用详解

《Python字典(Dictionary)使用详解》字典是python中最重要,最常用的数据结构之一,它提供了高效的键值对存储和查找能力,:本文主要介绍Python字典(Dictionary)... 目录字典1.基本特性2.创建字典3.访问元素4.修改字典5.删除元素6.字典遍历7.字典的高级特性默认字典

Python自动化批量重命名与整理文件系统

《Python自动化批量重命名与整理文件系统》这篇文章主要为大家详细介绍了如何使用Python实现一个强大的文件批量重命名与整理工具,帮助开发者自动化这一繁琐过程,有需要的小伙伴可以了解下... 目录简介环境准备项目功能概述代码详细解析1. 导入必要的库2. 配置参数设置3. 创建日志系统4. 安全文件名处

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

Python包管理工具pip的升级指南

《Python包管理工具pip的升级指南》本文全面探讨Python包管理工具pip的升级策略,从基础升级方法到高级技巧,涵盖不同操作系统环境下的最佳实践,我们将深入分析pip的工作原理,介绍多种升级方... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函