Python编程使用tkinter模块实现计算器软件完整代码示例


Posted in Python onNovember 29, 2017

Python 提供了多个图形开发界面的库。Tkinter就是其中之一。 Tkinter 模块(Tk 接口)是 Python 的标准 Tk GUI 工具包的接口 .Tk 和 Tkinter 可以在大多数的 Unix 平台下使用,同样可以应用在 Windows 和 Macintosh 系统里。Tk8.0 的后续版本可以实现本地窗口风格,并良好地运行在绝大多数平台中。

该计算器使用Python tkinter模块开发

效果如下图

Python编程使用tkinter模块实现计算器软件完整代码示例

import tkinter #导入tkinter模块

root = tkinter.Tk()
root.minsize(280,500)
root.title('李蛟龙的计算器')

#1.界面布局
#显示面板
result = tkinter.StringVar()
result.set(0)              #显示面板显示结果1,用于显示默认数字0
result2 = tkinter.StringVar()      #显示面板显示结果2,用于显示计算过程
result2.set('')
#显示版
label = tkinter.Label(root,font = ('微软雅黑',20),bg = '#EEE9E9',bd ='9',fg = '#828282',anchor = 'se',textvariable = result2)
label.place(width = 280,height = 170)
label2 = tkinter.Label(root,font = ('微软雅黑',30),bg = '#EEE9E9',bd ='9',fg = 'black',anchor = 'se',textvariable = result)
label2.place(y = 170,width = 280,height = 60)

#数字键按钮

btn7 = tkinter.Button(root,text = '7',font = ('微软雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('7'))
btn7.place(x = 0,y = 285,width = 70,height = 55)
btn8 = tkinter.Button(root,text = '8',font = ('微软雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('8'))
btn8.place(x = 70,y = 285,width = 70,height = 55)
btn9 = tkinter.Button(root,text = '9',font = ('微软雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('9'))
btn9.place(x = 140,y = 285,width = 70,height = 55)

btn4 = tkinter.Button(root,text = '4',font = ('微软雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('4'))
btn4.place(x = 0,y = 340,width = 70,height = 55)
btn5 = tkinter.Button(root,text = '5',font = ('微软雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('5'))
btn5.place(x = 70,y = 340,width = 70,height = 55)
btn6 = tkinter.Button(root,text = '6',font = ('微软雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('6'))
btn6.place(x = 140,y = 340,width = 70,height = 55)

btn1 = tkinter.Button(root,text = '1',font = ('微软雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('1'))
btn1.place(x = 0,y = 395,width = 70,height = 55)
btn2 = tkinter.Button(root,text = '2',font = ('微软雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('2'))
btn2.place(x = 70,y = 395,width = 70,height = 55)
btn3 = tkinter.Button(root,text = '3',font = ('微软雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('3'))
btn3.place(x = 140,y = 395,width = 70,height = 55)
btn0 = tkinter.Button(root,text = '0',font = ('微软雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('0'))
btn0.place(x = 70,y = 450,width = 70,height = 55)

#运算符号按钮
btnac = tkinter.Button(root,text = 'AC',bd = 0.5,font = ('黑体',20),fg = 'orange',command = lambda :pressCompute('AC'))
btnac.place(x = 0,y = 230,width = 70,height = 55)
btnback = tkinter.Button(root,text = '←',font = ('微软雅黑',20),fg = '#4F4F4F',bd = 0.5,command = lambda:pressCompute('b'))
btnback.place(x = 70,y = 230,width = 70,height = 55)
btndivi = tkinter.Button(root,text = '÷',font = ('微软雅黑',20),fg = '#4F4F4F',bd = 0.5,command = lambda:pressCompute('/'))
btndivi.place(x = 140,y = 230,width = 70,height = 55)
btnmul = tkinter.Button(root,text ='×',font = ('微软雅黑',20),fg = "#4F4F4F",bd = 0.5,command = lambda:pressCompute('*'))
btnmul.place(x = 210,y = 230,width = 70,height = 55)
btnsub = tkinter.Button(root,text = '-',font = ('微软雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda:pressCompute('-'))
btnsub.place(x = 210,y = 285,width = 70,height = 55)
btnadd = tkinter.Button(root,text = '+',font = ('微软雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda:pressCompute('+'))
btnadd.place(x = 210,y = 340,width = 70,height = 55)
btnequ = tkinter.Button(root,text = '=',bg = 'orange',font = ('微软雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda :pressEqual())
btnequ.place(x = 210,y = 395,width = 70,height = 110)
btnper = tkinter.Button(root,text = '%',font = ('微软雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda:pressCompute('%'))
btnper.place(x = 0,y = 450,width = 70,height = 55)
btnpoint = tkinter.Button(root,text = '.',font = ('微软雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda:pressCompute('.'))
btnpoint.place(x = 140,y = 450,width = 70,height = 55)

#操作函数
lists = []              #设置一个变量 保存运算数字和符号的列表
isPressSign = False         #添加一个判断是否按下运算符号的标志,假设默认没有按下按钮
isPressNum = False
#数字函数
def pressNum(num):          #设置一个数字函数 判断是否按下数字 并获取数字将数字写在显示版上
  global lists           #全局化lists和按钮状态isPressSign
  global isPressSign
  if isPressSign == False:
    pass
  else:              #重新将运算符号状态设置为否
    result.set(0)
    isPressSign = False

  #判断界面的数字是否为0
  oldnum = result.get()       #第一步
  if oldnum =='0':         #如过界面上数字为0 则获取按下的数字
    result.set(num)
  else:              #如果界面上的而数字不是0 则链接上新按下的数字
    newnum = oldnum + num
    result.set(newnum)      #将按下的数字写到面板中

#运算函数
def pressCompute(sign):
  global lists
  global isPressSign
  num = result.get()       #获取界面数字
  lists.append(num)        #保存界面获取的数字到列表中

  lists.append(sign)       #讲按下的运算符号保存到列表中
  isPressSign = True

  if sign =='AC':        #如果按下的是'AC'按键,则清空列表内容,讲屏幕上的数字键设置为默认数字0
    lists.clear()
    result.set(0)
  if sign =='b':         #如果按下的是退格‘',则选取当前数字第一位到倒数第二位
    a = num[0:-1]
    lists.clear()
    result.set(a)

#获取运算结果函数
def pressEqual():
  global lists
  global isPressSign

  curnum = result.get()      #设置当前数字变量,并获取添加到列表
  lists.append(curnum)

  computrStr = ''.join(lists)   #讲列表内容用join命令将字符串链接起来
  endNum = eval(computrStr)    #用eval命令运算字符串中的内容
#  a = str(endNum)
#  b = '='+a            #给运算结果前添加一个 ‘=' 显示  不过这样写会有BUG 不能连续运算,这里注释,不要 =
#  c = b[0:10]           #所有的运算结果取9位数
  result.set(endNum)          #讲运算结果显示到屏幕1
  result2.set(computrStr)     #将运算过程显示到屏幕2
  lists.clear()          #清空列表内容

root.mainloop()

总结

以上就是本文关于Python编程使用tkinter模块实现计算器软件完整代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
Python ZipFile模块详解
Nov 01 Python
Python设计模式之单例模式实例
Apr 26 Python
Python中字典的setdefault()方法教程
Feb 07 Python
使用Django启动命令行及执行脚本的方法
May 29 Python
numpy判断数值类型、过滤出数值型数据的方法
Jun 09 Python
Python统计纯文本文件中英文单词出现个数的方法总结【测试可用】
Jul 25 Python
解决Pycharm运行时找不到文件的问题
Oct 29 Python
Python Flask框架扩展操作示例
May 03 Python
python求最大值,不使用内置函数的实现方法
Jul 09 Python
python实现复制大量文件功能
Aug 31 Python
ubuntu16.04升级Python3.5到Python3.7的方法步骤
Aug 20 Python
解决Pytorch dataloader时报错每个tensor维度不一样的问题
May 28 Python
Python科学画图代码分享
Nov 29 #Python
Python中Scrapy爬虫图片处理详解
Nov 29 #Python
Python使用django框架实现多人在线匿名聊天的小程序
Nov 29 #Python
Python实现的计数排序算法示例
Nov 29 #Python
Scrapy框架CrawlSpiders的介绍以及使用详解
Nov 29 #Python
pycharm下打开、执行并调试scrapy爬虫程序的方法
Nov 29 #Python
Python快速排序算法实例分析
Nov 29 #Python
You might like
ThinkPHP使用心得分享-分页类Page的用法
2014/05/15 PHP
微信自定义菜单的处理开发示例
2015/04/16 PHP
微信公众号判断用户是否已关注php代码解析
2016/06/24 PHP
PHP Cookie学习笔记
2016/08/23 PHP
php实现往pdf中加数字签名操作示例【附源码下载】
2018/08/07 PHP
jQuery使用hide方法隐藏页面上指定元素的方法
2015/03/30 Javascript
node.js+express制作网页计算器
2016/01/17 Javascript
使用jquery获取url及url参数的简单实例
2016/06/14 Javascript
BootStrap+Angularjs+NgDialog实现模式对话框
2016/08/24 Javascript
JS简单获取客户端IP地址的方法【调用搜狐接口】
2016/09/05 Javascript
Vue项目中设置背景图片方法
2018/02/21 Javascript
实战node静态文件服务器的示例代码
2018/03/08 Javascript
python正则表达式抓取成语网站
2013/11/20 Python
Python随机生成信用卡卡号的实现方法
2015/05/14 Python
python创建临时文件夹的方法
2015/07/06 Python
简单实现python爬虫功能
2015/12/31 Python
Python进行数据提取的方法总结
2016/08/22 Python
MAC中PyCharm设置python3解释器
2017/12/15 Python
python机器学习案例教程——K最近邻算法的实现
2017/12/28 Python
python3+PyQt5使用数据库表视图
2018/04/24 Python
对python生成业务报表的实例详解
2019/02/03 Python
python通过paramiko复制远程文件及文件目录到本地
2019/04/30 Python
Python可变和不可变、类的私有属性实例分析
2019/05/31 Python
Python numpy数组转置与轴变换
2019/11/15 Python
解决python图像处理图像赋值后变为白色的问题
2020/06/04 Python
pycharm 的Structure界面设置操作
2021/02/05 Python
eBay荷兰购物网站:eBay.nl
2020/06/26 全球购物
个人找工作自荐信格式
2013/09/21 职场文书
自荐书格式
2013/12/01 职场文书
回门宴答谢词
2014/01/13 职场文书
九年级化学教学反思
2014/01/28 职场文书
煤矿安全协议书
2014/08/20 职场文书
离职感谢信
2015/01/21 职场文书
中班下学期个人工作总结
2015/02/12 职场文书
总结高并发下Nginx性能如何优化
2021/11/01 Servers
Windows11 Insider Preview Build 25206今日发布 更新内容汇总
2022/09/23 数码科技