#引入相关模块(math模块和tkinter模块)

importmathimporttkinterclassMyCalculator:#初始化对象

def __init__(self):#设置主界面

self.root =tkinter.Tk()

self.root.title('于淼的计算器')

self.root.minsize(400, 550)#为label标签设置一个变量

self.var =tkinter.StringVar()#设置初始值为0

self.var.set('0')#设置用于存储运算数据的列表

self.operation_lists =[]#设置空字符串,用于截取

self.metastrs =''

#设置是否按下运算符的变量

self.isoperation =False#调用设置界面的方法

self.set_main_interface()

self.root.mainloop()#主界面布局方法

defset_main_interface(self):#显示结果和操作的区域

#frame = tkinter.Frame(self.root, width=400, height=100, )

#frame.pack()

label = tkinter.Label(self.root, bg='white', textvariable=self.var, anchor='se', font=('黑体', 35), bd=10)

label.place(x=10, y=10, width=380, height=100)#设置数字按钮

button11 = tkinter.Button(self.root, text='%', font=('黑体', 20), command=self.percent)

button11.place( x=10, y=130, width=90, height=60)

button12= tkinter.Button(self.root,text='√', font=('黑体', 20), command=self.square)

button12.place(x=105, y=130, width=90, height=60)

button13= tkinter.Button(self.root,text='x²', font=('黑体', 20), command=self.power)

button13.place( x=200, y=130, width=90, height=60)

button14= tkinter.Button(self.root,text='1/x', font=('黑体', 20), command=self.backwords)

button14.place( x=295, y=130, width=90, height=60)

button21= tkinter.Button(self.root,text='CE', font=('黑体', 20), command=self.clear_CE)

button21.place( x=10, y=195, width=90, height=60)

button22= tkinter.Button(self.root,text='C', font=('黑体', 20), command=self.clear_C)

button22.place(x=105, y=195, width=90, height=60)

button23= tkinter.Button(self.root,text='←', font=('黑体', 20), command=self.strip)

button23.place(x=200, y=195, width=90, height=60)

button24= tkinter.Button(self.root,text='÷', font=('黑体', 20), command=lambda: self.symbol('/'))

button24.place(x=295, y=195, width=90, height=60)

button31= tkinter.Button(self.root,text='7', font=('黑体', 20), command=lambda: self.change('7'))

button31.place(x=10, y=260, width=90, height=60)

button32= tkinter.Button(self.root,text='8', font=('黑体', 20), command=lambda: self.change('8'))

button32.place(x=105, y=260, width=90, height=60)

button33= tkinter.Button(self.root,text='9', font=('黑体', 20), command=lambda: self.change('9'))

button33.place(x=200, y=260, width=90, height=60)

button34= tkinter.Button(self.root,text='×', font=('黑体', 20), command=lambda: self.symbol('*'))

button34.place(x=295, y=260, width=90, height=60)

button41= tkinter.Button(self.root,text='4', font=('黑体', 20), command=lambda: self.change('4'))

button41.place(x=10, y=325, width=90, height=60)

button42= tkinter.Button(self.root,text='5', font=('黑体', 20), command=lambda: self.change('5'))

button42.place(x=105, y=325, width=90, height=60)

button43= tkinter.Button(self.root,text='6', font=('黑体', 20), command=lambda: self.change('6'))

button43.place(x=200, y=325, width=90, height=60)

button44= tkinter.Button(self.root,text='-', font=('黑体', 20), command=lambda: self.symbol('-'))

button44.place(x=295, y=325, width=90, height=60)

button51= tkinter.Button(self.root,text='1', font=('黑体', 20), command=lambda: self.change('1'))

button51.place(x=10, y=390, width=90, height=60)

button52= tkinter.Button(self.root,text='2', font=('黑体', 20), command=lambda: self.change('2'))

button52.place(x=105, y=390, width=90, height=60)

button53= tkinter.Button(self.root,text='3', font=('黑体', 20), command=lambda: self.change('3'))

button53.place(x=200, y=390, width=90, height=60)

button54= tkinter.Button(self.root,text='+', font=('黑体', 20), command=lambda: self.symbol('+'))

button54.place(x=295, y=390, width=90, height=60)

button61= tkinter.Button(self.root,text='±', font=('黑体', 20), command=self.opposite_num)

button61.place(x=10, y=455, width=90, height=60)

button62= tkinter.Button(self.root,text='0', font=('黑体', 20), command=lambda: self.change('0'))

button62.place(x=105, y=455, width=90, height=60)

button63= tkinter.Button(self.root,text='.', font=('黑体', 20), command=lambda: self.change('.'))

button63.place( x=200, y=455, width=90, height=60)

button64= tkinter.Button(self.root,text='=', font=('黑体', 20), command=self.equal)

button64.place(x=295, y=455, width=90, height=60)#计算器各种操作的函数

#按下数字的函数

defchange(self,num):#全局化

globalisoperation#判断是否按下了运算符号

#如果按下运算符号

ifself.isoperation:#将面板中的数据归0

self.var.set('0')#设置运算符状态

self.isoperation =False#获取原来的数据

metadata =self.var.get()#判断原数字是否为0

if metadata == '0' and num == '.': #如果为0,判断输入的数据是否为符号’.'

self.var.set('0'+num)elif metadata =='0' and num != '.' :#将按下的数字显示到标签中

self.var.set(num)elif metadata in ['除数不能为0','无效输入','结果未定义']:

self.var.set(num)#如果不是0,判断原数字中是否存在符号’.'

elif '.' in metadata and num == '.':return

#原数字不是0, 且符号'.' 不在原数字中

else:#将原有数字和当前按下的数字拼合到一起,显示到标签中

self.var.set(metadata +num)#获取面板中的数值,截取最后一个字符

foredata = self.var.get()[-1]#判断操作列表中的元素

#如果操作列表不为空,且操作列表最后一元素为运算符号

if self.operation_lists != [] and self.operation_lists[-1] in ['+','-','*','/']:#将面板中的字符加入列表

self.operation_lists.append(foredata)#print(self.operation_lists)

else:#如果操作列表为空

if self.operation_lists ==[] :#将截取的字符串加入列表

self.operation_lists.append(foredata)#print(self.operation_lists)

else:#如果操作列表不为空,更新列表最后一个元素

self.operation_lists[-1] +=foredata#print(self.operation_lists)

#按下运算符号的函数

defsymbol(self,sign):#全局化

globalisoperation#判断列表是否为空,不为空时,最后输入的是否为运算符

if self.operation_lists != [] and self.operation_lists[-1] in ['+','-','*','/']:

self.operation_lists[-1] =signreturn

#如果列表为空,将当前面板中的数据加入到列表中

elif self.operation_lists ==[]:#获取面板中的数据

foredata =self.var.get()#将当前面板中的数据添加到列表中

self.operation_lists.append(foredata)#将列表中的数据连接成字符串,使用eval函数进行运算

result = eval(''.join(self.operation_lists))#将运算结果显示到面板中

self.var.set(result)#清空列表,将运算结果添加到列表中

self.operation_lists.clear()

self.operation_lists.append(str(result))#将按下的运算符也存入列表中

self.operation_lists.append(sign)#设置运算符的状态

self.isoperation =True#按下等号的函数

defequal(self):#全局化

globalisoperation#判断除数是否为0

if '/' in self.operation_lists and self.operation_lists[-1] == '0':

self.var.set('除数不能为0')

self.operation_lists.clear()return

elif self.operation_lists!= [] and self.operation_lists[-1] in ['+','-','*','/']:

self.var.set('结果未定义')#如果为空列表

elif self.operation_lists ==[]:#获取当前面板中的值

value =self.var.get()#将面板中的值加入列表

self.operation_lists.append(value)

self.operation_lists+=self.metastrs#将列表中的数据组合成字符串,通过eval函数进行运算操作

strs_num = ''.join(self.operation_lists)

result=eval(strs_num)#判断整数位为长度是否大于13

if len(str(round((result)))) > 13:

self.var.set('溢出')else:

result= str(result)[0:13]#将运算结果显示到面板中

self.var.set(result)else:#将列表中的数据组合成字符串,通过eval函数进行运算操作

strs_num = ''.join(self.operation_lists)

result=eval(strs_num)#判断整数位为长度是否大于13

if len(str(round((result)))) > 13:

self.var.set('溢出')else:

result= str(result)[0:13]#将运算结果显示到面板中

self.var.set(result)

self.metastrs= self.operation_lists[-2:]#print(self.metastrs)

#清空操作列表,便于下次运算使用

self.operation_lists.clear()#设置运算符状态

self.isoperation =True#设置清空操作:计算器中的C

defclear_C(self):#将面板中的数据添加到列表里

self.operation_lists.append(self.var.get())#清空用于存储运算数据的列表

self.operation_lists.clear()#print(self.operation_lists)

#将面板中的数据归零

self.var.set('0')#设置删除当前数据的操作:计算器中的CE

defclear_CE(self):#清空用于存储运算数据的列表

self.operation_lists.pop()#print(self.operation_lists)

#将面板中的数据归零

self.var.set('0')#设置删除键

defstrip(self):#获取当前面板中的数据

present_data =self.var.get()print(present_data)#判断面板中的数据是否为0

if present_data == '0':return

#设置删除数据的操作

present_data = present_data[:len(present_data)-1]print(present_data)#判断删除后的字符串是否为空

if present_data =='':#如果为空,设置为0

self.var.set('0')

self.operation_lists[-1] = ''

else:#如果不为空,将删除后的数据显示到面板中

self.var.set(present_data)

self.operation_lists[-1] =present_data#设置1/x键

defbackwords(self):#全局化

globalisoperation#获取当前面板中的数据

globalpresent_data

present_data=self.var.get()#判断列表中数据运算结果是否为0

if present_data == '0':

self.var.set('除数不能为0')else:#将当前数据取倒数,设置到面板中

data = '(1' + '/' + present_data+')'

##判断整数位为长度是否大于13

if len(str(round((eval(data))))) > 13:

self.var.set('溢出')else:

result= str(eval(data))[0:12]

self.var.set(result)#print(self.operation_lists)

#将操作列表中的最后一个元素更新为倒数的结果

self.operation_lists[-1] =str(eval(data))print(self.operation_lists)#设置运算符状态

self.isoperation =True#设置一个数的平方的函数

defpower(self):#全局化

globalisoperation#获取当前面板中的数据

present_data =self.var.get()#print(present_data)

#将当前数据进行平方运算后,设置到面板中

data = 'math.pow' + '(' + present_data +','+ '2' +')'

#判断整数位为长度是否大于13

if len(str(round(eval(data)))) > 13:

self.var.set('溢出')else:

result= str(eval(data))[0:12]

self.var.set(result)

self.operation_lists[-1] =str(eval(data))#设置运算符状态

self.isoperation =True#设置一个数据的开平方运算

defsquare(self):#全局化

globalisoperation#获取当前面板中的数据

present_data =self.var.get()#判断数值的正负

if present_data[0] == '-':

self.var.set('无效输入')else:#将当前数据进行平方运算后,设置到面板中

data = 'math.sqrt' + '(' + present_data +')'result= str(eval(data))[0:12]

self.var.set(result)

self.operation_lists[-1] =str(eval(data))#设置运算符状态

self.isoperation =True#设置’%‘的运算

defpercent(self):#全局化

globalisoperation#获取当前面板中的数据

present_data =self.var.get()#将当前数据进行%运算后,设置到面板中

data = present_data + '*' +'0.01'result= str(eval(data))[0:12]

self.var.set(result)

self.operation_lists[-1] =str(eval(data))#设置运算符状态

self.isoperation =True#求反的运算

defopposite_num(self):#全局化

globalisoperation#获取当前面板中的数据

present_data =self.var.get()if present_data[0] == '-':

self.var.set(present_data[1:])elif present_data[-1] == '0':

self.var.set('0')else:

self.var.set('-' +present_data)

self.operation_lists[-1] =self.var.get()print(self.operation_lists)#设置运算符状态

self.isoperation =True#实例化对象

my= MyCalculator()

用python的tkiner写计算器_Python(10)--利用tkinter模块实现一个简单的计算器功能相关推荐

  1. 使用Python中内置tkinter模块做一个简易的计算器

    当前环境: windows + python3.7 + tkinter 模块 一.总体规划 1.需要用到的模块有  tkinter 2.tkinter 模块中需要用到的知识点有: 2.1.创建主窗口 ...

  2. python怎么打开h5文件_python中利用h5py模块读取h5文件中的主键方法

    如下所示: import h5py import numpy as np #HDF5的写入: imgData = np.zeros((2,4)) f = h5py.File('HDF5_FILE.h5 ...

  3. python中xlrd写操作_Python读写操作Excel模块_xlrd_xlwt_xlutils

    Python 读写操作Excel -- 安装第三方库(xlrd.xlwt.xlutils.openpyxl) 如果仅仅是要以表单形式保存数据,可以借助 CSV 格式(一种以逗号分隔的表格数据格式)进行 ...

  4. java 做计算器 百度云_用Java做一个简单的计算器

    窗体 package Calc; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * * 计算器程序6261 ...

  5. 小学科学作业计算器c语言,怎样用C实现一个简单科学计算器

    写了两个小时写的很粗陋,只能计算整数,但是可以支持多级别括号,和同一级别的多个括号. 希望大家多多指教,这个题目其实很有意思,要是大家有兴趣,可以把功能做的更加全面,比如算次方什么的 #include ...

  6. python123程序设计题说句心里话_用c++写一个简单的计算器程序

    // 050305.cpp : 定义控制台应用程序的入口点. // // 050304.cpp : 定义控制台应用程序的入口点. // //四则运算 #include "stdafx.h&q ...

  7. python计算器教程,用Python程序制作一个简单的计算器

    用Python程序制作一个简单的计算器 在此示例中,您将学习创建一个简单的计算器,该计算器可以根据用户的输入进行加,减,乘或除. 要理解此示例,您应该了解以下Python编程主题: 通过函数创建简单计 ...

  8. 使用python做一个简单的计算器

    今天教大家如何使用python撸一个简单的计算器小程序. 具体源码如下: import tkinter import math import tkinter.messageboxclass Calcu ...

  9. 用python编写一个简单的计算器

    计算器功能与组成部分 基本功能 创建简单的操作界面 键盘快捷键 屏幕和显示部分 数字按钮和功能键 负号和小数点的输入 在数据中插入逗号 核心计算部分 次方运算 返回和清除 自定义设置 基本功能 一个简 ...

最新文章

  1. oracle从备份提取归档,Oracle归档模式有备份,丢失数据文件的恢复
  2. VSCode输出框中文乱码问题
  3. 驱动列举进程输出到应用层
  4. docker容器间双向通信(基于Bridge网桥)
  5. GitHub之GitHub Actions的项目自动化持续集成和部署
  6. 前端面试题之http/HTML/浏览器(二)
  7. python提取txt中的字符串数据_python 从字符串中提取数值
  8. Public Sale【博弈】
  9. CSDN博客如何调整文字的字体、大小、颜色
  10. 分享Silverlight/WPF/Windows Phone/HTML5一周学习导读(2月6日-2月12日)
  11. Netty 5用户指南
  12. 【AI面试题】GBDT原理、如何用于分类、常用损失函数
  13. ThreatARMOR添加零日恶意软件防御功能
  14. C语言差分双向码编码,基于c语言的数字基带信号码型变换系统设计1.doc
  15. 信号与系统实验四 LTI系统的时域分析
  16. 设计师经常逛的色彩搭配网站—配色方案吧
  17. python reshape函数参数-1(X.reshape(X.shape[0], -1).T)
  18. (离散)设函数 f:A→B,g:B→C,证明:若g °f是满射,则g是满射.
  19. 如何听广播来学计算机,MAC使用技巧之苹果itunes如何收听国内的广播?
  20. c语言编程基础心得,C语言编程学习心得体会

热门文章

  1. 有赞零售小票打印跨平台解决方案 1
  2. 程序员提高代码编译速度,都怎么做的?
  3. 我女朋友觉得这个标题土,我觉得很贴合中心思想啊!---- 人工智能的前世今生(完结篇)
  4. SpringBoot使用通配符加载配置文件
  5. RecyclerView 踩坑记
  6. 3983. 乾坤大挪移
  7. 对力螺旋(wrench)的理解
  8. 基于java个人财务账薄管理系统
  9. Ubuntu18.04顶部菜单消失
  10. Linux安装Charm-crypto环境详细流程