利用python写一个小而美的计算器

1、UI设计

利用自带的库tkinter,命名为“计算机”:

import tkinter,time,decimal,math,string

root=tkinter.Tk()
root.title('计算器')

自带的库可以满足基本的开发,也可以使用qt设计器进行开发

运行后得到下图的界面框:

在这里插入图片描述

计算器的界面布局程序如下:

def buju(root):
    global cuncu, vartext, result, fuhao
    entry1 = tkinter.Label(root, width=30, height=2, bg='white', anchor='se', textvariable=vartext)
    entry1.grid(row=0, columnspan=5)
    buttonMC=tkinter.Button(root,text='MC',width=5)
    buttonMR=tkinter.Button(root,text='MR',width=5)
    buttonMS=tkinter.Button(root,text='MS',width=5)
    buttonM1=tkinter.Button(root,text='M+',width=5)
    buttonM2=tkinter.Button(root,text='M-',width=5)
    buttonMC.grid(row=1,column=0)
    buttonMR.grid(row=1,column=1)
    buttonMS.grid(row=1,column=2)
    buttonM1.grid(row=1,column=3)
    buttonM2.grid(row=1,column=4)


    buttonJ=tkinter.Button(root,text='←',width=5,command=anjianzhi('c').tui)
    buttonCE=tkinter.Button(root,text='CE',width=5)
    buttonC=tkinter.Button(root,text=' C ',width=5,command=anjianzhi('c').clear)
    button12=tkinter.Button(root,text='±',width=5,command=anjianzhi('c').zhengfu)
    buttonD=tkinter.Button(root,text='√',width=5,command=anjianzhi('sqrt').yunshuan)
    buttonJ.grid(row=2,column=0)
    buttonCE.grid(row=2,column=1)
    buttonC.grid(row=2,column=2)
    button12.grid(row=2,column=3)
    buttonD.grid(row=2,column=4)

    button7=tkinter.Button(root,text=' 7 ',width=5,command=anjianzhi('7').jia)
    button8=tkinter.Button(root,text=' 8 ',width=5,command=anjianzhi('8').jia)
    button9=tkinter.Button(root,text=' 9 ',width=5,command=anjianzhi('9').jia)
    buttonc=tkinter.Button(root, text=' / ',width=5,command=anjianzhi('/').yunshuan)
    buttonf= tkinter.Button(root, text=' % ',width=5)
    button7.grid(row=3,column=0)
    button8.grid(row=3,column=1)
    button9.grid(row=3,column=2)
    buttonc.grid(row=3,column=3)
    buttonf.grid(row=3,column=4)

    button4=tkinter.Button(root,text=' 4 ',width=5,command=anjianzhi('4').jia)
    button5=tkinter.Button(root,text=' 5 ',width=5,command=anjianzhi('5').jia)
    button6=tkinter.Button(root,text=' 6 ',width=5,command=anjianzhi('6').jia)
    buttonx=tkinter.Button(root,text=' * ',width=5,command=anjianzhi('*').yunshuan)
    buttonfs=tkinter.Button(root,text='1/x',width=5,command=anjianzhi('1/x').yunshuan)
    button4.grid(row=4,column=0)
    button5.grid(row=4,column=1)
    button6.grid(row=4,column=2)
    buttonx.grid(row=4,column=3)
    buttonfs.grid(row=4,column=4)

    button1 = tkinter.Button(root, text=' 1 ',width=5,command=anjianzhi('1').jia)
    button2 = tkinter.Button(root, text=' 2 ',width=5,command=anjianzhi('2').jia)
    button3 = tkinter.Button(root, text=' 3 ',width=5,command=anjianzhi('3').jia)
    button_= tkinter.Button(root, text=' - ',width=5,command=anjianzhi('-').yunshuan)
    buttondy= tkinter.Button(root, text=' \n = \n ',width=5,command=anjianzhi('=').yunshuan)
    button1.grid(row=5, column=0)
    button2.grid(row=5, column=1)
    button3.grid(row=5, column=2)
    button_.grid(row=5, column=3)
    buttondy.grid(row=5, column=4,rowspan=2)

    button0=tkinter.Button(root,text='   0   ',width=11,command=anjianzhi('0').jia)
    buttonjh = tkinter.Button(root,text=' . ',width=5,command=anjianzhi('c').xiaoshudian)
    buttonjia=tkinter.Button(root,text=' + ',width=5,command=anjianzhi('+').yunshuan)
    button0.grid(row=6,column=0,columnspan=2)
    buttonjh.grid(row=6,column=2)
    buttonjia.grid(row=6,column=3)

2、计算器实现逻辑

小数点实现:

def xiaoshudian(self):
    if cuncu.count('.') >= 1:
        pass
    else:
        if cuncu == [] :
            cuncu.append('0')
        cuncu.append('.')
        vartext.set(''.join(cuncu))

正负实现:

def zhengfu(self):
    if cuncu[0]:
        if cuncu[0] == '-':
            cuncu[0] = '+'
        elif cuncu[0] == '+':
            cuncu[0] = '-'
        else:
            cuncu.insert(0, '-')
    vartext.set(''.join(cuncu))

启动后的界面如下:

在这里插入图片描述

完整代码只有100多行,已上传github,可以复制链接到浏览器打开:

https://github.com/Baymini-zx/Caculater

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值