用python设计一个具有四则运算且运算位可以是小数和整数且有人机界面的计算器

时间: 2024-03-21 09:37:55 浏览: 26
好的,我可以帮你实现这个计算器。我们可以使用Python的GUI库Tkinter来创建人机界面,并通过对用户输入的字符串进行解析来实现四则运算。以下是代码示例: ```python from tkinter import * from decimal import Decimal root = Tk() root.title("四则运算计算器") # 创建文本框 input_box = Entry(root, width=35, borderwidth=5) input_box.grid(row=0, column=0, columnspan=4, padx=10, pady=10) # 处理按钮点击事件 def button_click(number): current = input_box.get() input_box.delete(0, END) input_box.insert(0, str(current) + str(number)) def button_clear(): input_box.delete(0, END) def button_add(): first_number = input_box.get() global f_num global math_operation math_operation = "addition" f_num = Decimal(first_number) input_box.delete(0, END) def button_subtract(): first_number = input_box.get() global f_num global math_operation math_operation = "subtraction" f_num = Decimal(first_number) input_box.delete(0, END) def button_multiply(): first_number = input_box.get() global f_num global math_operation math_operation = "multiplication" f_num = Decimal(first_number) input_box.delete(0, END) def button_divide(): first_number = input_box.get() global f_num global math_operation math_operation = "division" f_num = Decimal(first_number) input_box.delete(0, END) def button_decimal(): current = input_box.get() if "." not in current: input_box.delete(0, END) input_box.insert(0, str(current) + ".") def button_equal(): second_number = Decimal(input_box.get()) input_box.delete(0, END) if math_operation == "addition": result = f_num + second_number elif math_operation == "subtraction": result = f_num - second_number elif math_operation == "multiplication": result = f_num * second_number elif math_operation == "division": result = f_num / second_number input_box.insert(0, result) # 创建按钮 button_1 = Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1)) button_2 = Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2)) button_3 = Button(root, text="3", padx=40, pady=20, command=lambda: button_click(3)) button_4 = Button(root, text="4", padx=40, pady=20, command=lambda: button_click(4)) button_5 = Button(root, text="5", padx=40, pady=20, command=lambda: button_click(5)) button_6 = Button(root, text="6", padx=40, pady=20, command=lambda: button_click(6)) button_7 = Button(root, text="7", padx=40, pady=20, command=lambda: button_click(7)) button_8 = Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8)) button_9 = Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9)) button_0 = Button(root, text="0", padx=40, pady=20, command=lambda: button_click(0)) button_decimal = Button(root, text=".", padx=41, pady=20, command=button_decimal) button_add = Button(root, text="+", padx=39, pady=20, command=button_add) button_subtract = Button(root, text="-", padx=41, pady=20, command=button_subtract) button_multiply = Button(root, text="*", padx=40, pady=20, command=button_multiply) button_divide = Button(root, text="/", padx=41, pady=20, command=button_divide) button_equal = Button(root, text="=", padx=91, pady=20, command=button_equal) button_clear = Button(root, text="清空", padx=79, pady=20, command=button_clear) # 将按钮放置到界面上 button_1.grid(row=3, column=0) button_2.grid(row=3, column=1) button_3.grid(row=3, column=2) button_4.grid(row=2, column=0) button_5.grid(row=2, column=1) button_6.grid(row=2, column=2) button_7.grid(row=1, column=0) button_8.grid(row=1, column=1) button_9.grid(row=1, column=2) button_0.grid(row=4, column=0) button_decimal.grid(row=4, column=1) button_clear.grid(row=4, column=2) button_add.grid(row=1, column=3) button_subtract.grid(row=2, column=3) button_multiply.grid(row=3, column=3) button_divide.grid(row=4, column=3) button_equal.grid(row=5, column=2, columnspan=2) root.mainloop() ``` 这个计算器支持小数和整数四则运算,界面与普通计算器差不多,你可以基于这个示例进一步优化和扩展。

相关推荐

最新推荐

recommend-type

用Python做的数学四则运算_算术口算练习程序(后添加减乘除)

主要介绍了用Python做的数学四则运算_算术口算练习程序(后添加减乘除),需要的朋友可以参考下
recommend-type

python 实现简单的计算器(gui界面)

运行效果: 完整代码 from tkinter import * def click(num): global op op=op+str(num) iptext.set(op) def evaluate(): global op output=str(eval(op)) iptext.set(output) ...ipa
recommend-type

Python编程实现数学运算求一元二次方程的实根算法示例

主要介绍了Python编程实现数学运算求一元二次方程的实根算法,涉及Python数学运算求解方程的相关实现技巧,需要的朋友可以参考下
recommend-type

用python+pyqt5手工编写一个含交互界面的简易的词法分析器

python+pyqt5手工编写一个含交互界面的简易词法分析器 @author:x1nge. 编译原理基础实验 基础 在之前的一篇博文中我记录了一个不含交互界面的简易词法分析器程序编写内容 点击此处查看 在本文我将用pyqt5写一个...
recommend-type

人机交互程序 python实现人机对话

主要为大家详细介绍了人机交互程序,初步实现python人机对话,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用

![MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用](https://img-blog.csdnimg.cn/2020050917173284.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2thbmdqaWVsZWFybmluZw==,size_16,color_FFFFFF,t_70) # 1. MATLAB取整函数概述** MATLAB取整函数是一组强大的工具,用于对数值进行
recommend-type

我想做python的算法工程师,我应该学什么?学习的顺序是什么?网上有什么推荐的免费课程吗?回答具体精确一点不要太笼统

对于想要成为 Python 算法工程师的人来说,他们应该先从掌握 Python 的基础语法开始,然后学习数据结构和算法,并且要深入了解 Python 中的一些科学计算和数据处理库,比如 NumPy、Pandas 等。 学习的顺序可以是先学习基础语法和编程技巧,然后再学习数据结构和算法相关的知识,最后深入了解数据处理和科学计算相关的库。 对于免费课程的推荐,我建议你可以先去 Coursera、edX、Udacity 等网站上寻找相关课程,这些网站上有很多优质的 Python 编程和算法课程,你可以根据自己的需求和学习进度进行选择。此外,还可以考虑一些在线编程网站,如 HackerRank、L
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。