小朋友学Python(30):实现带UI的简易计算器(下)

一、最终效果

8.png

二、完整程序

import tkinter #导入tkinter模块
#定义数值
w = 280                    #整个界面的宽度
h = 480                    #整个界面的高度
processH = 120            #存放计算过程的Label的高度
resultH = 60            #存放计算结果的Label的高度
btnWidth = 70            #按纽宽度
btnHeight = 60            #按纽高度
msfont = '微软雅黑'        #微软雅黑字体
fontSize = 20            #字体大小
btnFgColor = '#4F4F4F'    #按纽颜色
btnBorderWidth = 0.5    #按纽边框粗细
#面板设置
root = tkinter.Tk()
root.minsize(w,h)                        #面板大小的最小值
root.title('海天一树的计算器')            #面板的标题
process = tkinter.StringVar()           #字符串用于存放计算过程,初始状态为空
process.set('')
result = tkinter.StringVar()            #字符串用于存放计算结果,初始状态为0
result.set(0)                           
#labelProcess用于显示计算过程,labelResult用于显示计算结果
labelProcess = tkinter.Label(root,font = (msfont,fontSize),bg = '#EEE9E9',bd ='9',fg = '#828282',anchor = 'se',textvariable = process)
labelProcess.place(width = w,height = processH)
labelResult = tkinter.Label(root,font = (msfont,30),bg = '#EEE9E9',bd ='9',fg = 'black',anchor = 'se',textvariable = result)
labelResult.place(y = processH,width = w,height = resultH)
#数字按键
btn7 = tkinter.Button(root,text = '7',font = (msfont,fontSize),fg = (btnFgColor),bd = btnBorderWidth,command = lambda : clickNum('7'))
btn7.place(x = 0,y = processH + resultH + btnHeight,width = btnWidth,height = btnHeight)
btn8 = tkinter.Button(root,text = '8',font = (msfont,fontSize),fg = (btnFgColor),bd = btnBorderWidth,command = lambda : clickNum('8'))
btn8.place(x = btnWidth,y = processH + resultH + btnHeight,width = btnWidth,height = btnHeight)
btn9 = tkinter.Button(root,text = '9',font = (msfont,fontSize),fg = (btnFgColor),bd = btnBorderWidth,command = lambda : clickNum('9'))
btn9.place(x = 2 * btnWidth,y = processH + resultH + btnHeight,width = btnWidth,height = btnHeight)
btn4 = tkinter.Button(root,text = '4',font = (msfont,fontSize),fg = (btnFgColor),bd = btnBorderWidth,command = lambda : clickNum('4'))
btn4.place(x = 0,y = processH + resultH + 2 * btnHeight,width = btnWidth,height = btnHeight)
btn5 = tkinter.Button(root,text = '5',font = (msfont,fontSize),fg = (btnFgColor),bd = btnBorderWidth,command = lambda : clickNum('5'))
btn5.place(x = btnWidth,y = processH + resultH + 2 * btnHeight,width = btnWidth,height = btnHeight)
btn6 = tkinter.Button(root,text = '6',font = (msfont,fontSize),fg = (btnFgColor),bd = btnBorderWidth,command = lambda : clickNum('6'))
btn6.place(x = 2 * btnWidth,y = processH + resultH + 2 * btnHeight,width = btnWidth,height = btnHeight)
btn1 = tkinter.Button(root,text = '1',font = (msfont,fontSize),fg = (btnFgColor),bd = btnBorderWidth,command = lambda : clickNum('1'))
btn1.place(x = 0,y = processH + resultH + 3 * btnHeight,width = btnWidth,height = btnHeight)
btn2 = tkinter.Button(root,text = '2',font = (msfont,fontSize),fg = (btnFgColor),bd = btnBorderWidth,command = lambda : clickNum('2'))
btn2.place(x = btnWidth,y = processH + resultH + 3 * btnHeight,width = btnWidth,height = btnHeight)
btn3 = tkinter.Button(root,text = '3',font = (msfont,fontSize),fg = (btnFgColor),bd = btnBorderWidth,command = lambda : clickNum('3'))
btn3.place(x = 2 * btnWidth,y = processH + resultH + 3 * btnHeight,width = btnWidth,height = btnHeight)
btn0 = tkinter.Button(root,text = '0',font = (msfont,fontSize),fg = (btnFgColor),bd = btnBorderWidth,command = lambda : clickNum('0'))
btn0.place(x = btnWidth,y = processH + resultH + 4 * btnHeight,width = btnWidth,height = btnHeight)
#运算符号按键
btnac = tkinter.Button(root,text = 'AC',bd = btnBorderWidth,font = ('黑体',fontSize),fg = 'orange',command = lambda :clickSign('AC'))
btnac.place(x = 0,y = processH + resultH,width = btnWidth,height = btnHeight)
btnback = tkinter.Button(root,text = '←',font = (msfont,fontSize),fg = btnFgColor,bd = btnBorderWidth,command = lambda:clickSign('b'))
btnback.place(x = btnWidth,y = processH + resultH,width = btnWidth,height = btnHeight)
btndiv = tkinter.Button(root,text = '÷',font = (msfont,fontSize),fg = btnFgColor,bd = btnBorderWidth,command = lambda:clickSign('/'))
btndiv.place(x = 2 * btnWidth,y = processH + resultH,width = btnWidth,height = btnHeight)
btnmul = tkinter.Button(root,text ='×',font = (msfont,fontSize),fg = btnFgColor,bd = btnBorderWidth,command = lambda:clickSign('*'))
btnmul.place(x = 3 * btnWidth,y = processH + resultH,width = btnWidth,height = btnHeight)
btnsub = tkinter.Button(root,text = '-',font = (msfont,fontSize),fg = (btnFgColor),bd = btnBorderWidth,command = lambda:clickSign('-'))
btnsub.place(x = 3 * btnWidth,y = processH + resultH + btnHeight,width = btnWidth,height = btnHeight)
btnadd = tkinter.Button(root,text = '+',font = (msfont,fontSize),fg = (btnFgColor),bd = btnBorderWidth,command = lambda:clickSign('+'))
btnadd.place(x = 3 * btnWidth,y = processH + resultH + 2 * btnHeight,width = btnWidth,height = btnHeight)
btnequ = tkinter.Button(root,text = '=',bg = 'orange',font = (msfont,fontSize),fg = (btnFgColor),bd = btnBorderWidth,command = lambda :clickEqual())
btnequ.place(x = 3 * btnWidth,y = processH + resultH + 3 * btnHeight,width = btnWidth,height = 2 * btnHeight)
btnper = tkinter.Button(root,text = '%',font = (msfont,fontSize),fg = (btnFgColor),bd = btnBorderWidth,command = lambda:clickPer())
btnper.place(x = 0,y = processH + resultH + 4 * btnHeight,width = btnWidth,height = btnHeight)
btnpoint = tkinter.Button(root,text = '.',font = (msfont,fontSize),fg = (btnFgColor),bd = btnBorderWidth,command = lambda:clickNum('.'))
btnpoint.place(x = 2 * btnWidth,y = processH + resultH + 4 * btnHeight,width = btnWidth,height = btnHeight)
#点击数字
lists = []                             #数组,保存数字和运算符
isSignClicked = False                  #是否点击了符号按纽
isEqualClicked = False                #是否点击了等号按纽
def clickNum(num):                   
    global lists                    #将lists变量设为全局变量
    global isSignClicked            #将isSignClicked变量设为全局变量
    global isEqualClicked
    if True == isSignClicked:                   
        result.set(0)                #如果上次按过运算符,那么这次要把原先的数字清空,否则会加在上次那个数的末尾
        isSignClicked = False
    if True == isEqualClicked:
        result.set(0)
        isEqualClicked = True
    #判断界面的数字是否为0
    oldnum = result.get()              #第一步
    if '0' == oldnum:                  #如果界面上数字为0 则获取按下的数字
        result.set(num)
    else:                             #如果界面上的数字不为0  则链接上新按下的数字
        newnum = oldnum + num
        result.set(newnum)             #将按下的数字写到面板中
#点击运算符号(等号除外)
def clickSign(sign):
    global lists
    global isSignClicked
    num = result.get()              #获取界面数字
    if 0 == num.find('='):            #在连续计算时,若得到的是“=15”,要去掉等号,取其数字
        num = num[1:len(num)]
    lists.append(num)               #将界面获取的数字保存列表中
    lists.append(sign)              #将运算符号保存到列表中
    isSignClicked = True
    if 'AC' ==  sign:                  #如果按下的是'AC'按键,则清空列表内容,并将屏幕上的数字设置为默认值0
        lists.clear()
        process.set('')
        result.set(0)
    if 'b' ==  sign:                  #如果按下的是退格按纽,则选取当前数字第一位到倒数第二位
        a = num[0:-1]
        lists.clear()
        result.set(a)
        if 0 == len(a):                #如果是最后一个数,退格后变成0
            result.set(0)
#求百分值
def clickPer():
    curval = result.get()
    lists.append(curval)
    lists.append('/')
    lists.append('100')
    res = ''.join(lists)
    res = eval(res)
    result.set(res)
    lists.clear()
#获取运算结果
def clickEqual():
    global lists
    global isEqualClicked
    isEqualClicked = True
    curval = result.get()           #将界面上的数字添加到列表中
    lists.append(curval)
    proc = ''.join(lists)             #将列表内容用join命令将字符串链接起来
    res = eval(proc)                   #用eval命令运算字符串中的内容
    res = '='+str(res)              #给运算结果前添加一个 ‘=’ 显示 
    res = res[0:10]                 #所有的运算结果取9位数
    process.set(proc)                 #显示运算过程
    result.set(res)                 #显示运算结果
    lists.clear()                   #清空lists数组中的内容
root.mainloop()

三、Github源码下载

下载

原文发布于微信公众号 - 海天一树(gh_de7b45c40e8b)

原文发表时间:2018-03-04

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏十月梦想

CSS3 动画Animation的8大属性

animation复合属性。检索或设置对象所应用的动画特效。 如果有多个属性值时以”,”隔开,适用于所有元素,包含伪对象:after和:before

11210
来自专栏前端布道

为什么是link-visited-hover-active

前言 通常我们在设置链接的一些伪类(link,visited,hover,active)样式时,要让不同的状态显示正确的样式,我们需要按一定的顺序设置这些伪类的...

36550
来自专栏天天

background、转换、过渡

10430
来自专栏IMWeb前端团队

transform 的副作用

本文作者:IMWeb elvin 原文出处:IMWeb社区 未经同意,禁止转载 transform 想必大家都很熟悉,可以通过其转换(translat...

22490
来自专栏网络

简单的 canvas 翻角效果

由于工作需求,需要写一个翻角效果: ? demo链接:http://jsbin.com/rereqes。 右上角需要从无的状态撕开一个标记,且有动画过程,上图是...

24600
来自专栏HT

基于HT for Web的3D拓扑树的实现

在HT for Web中2D和3D应用都支持树状结构数据的展示,展现效果各异,2D上的树状结构在展现层级关系明显,但是如果数据量大的话,看起来就没那么直观,找到...

24350
来自专栏儿童编程

Python的艺术玩法——“孔雀开屏”篇

本文用Python实现一个“孔雀开屏”的效果,Python也可以这么玩。下面是源码,注释里面的是不同画面的执行代码。

23020
来自专栏游戏杂谈

as3绘制抛物线

一般做页游的过程中,特效的释放可能是不是固定位置的播放,是需要进行“运动的”(其实就是移动特效这个影响剪辑)。举个例子:步兵射箭,不确定箭发射的方向,事先也不...

11320
来自专栏IMWeb前端团队

前沿技术解密——VirtualDOM

作为React的核心技术之一Virtual DOM,一直披着神秘的面纱。 实际上,Virtual DOM包含: Javascript DOM模型树(VTre...

21490
来自专栏数据小魔方

查询与引用函数——offset函数

今天要跟大家分享的是一个强大的查询与引用函数——offset函数! OFFSET函数的语法参数相对比较多,但是因为参数位置和函数都比较固定,所以只要能够理解各自...

33870

扫码关注云+社区

领取腾讯云代金券