python简单计算器

计算器1.0

#coding=utf8
from Tkinter import *
from functools import partial as pt

top = Tk()
top.title('计算器1.0')
res = StringVar()
cc = StringVar()

e = Entry(top,relief = SUNKEN,textvariable = cc).pack(side = TOP,expand = YES,fill = BOTH)
r = Entry(top,relief = SUNKEN,textvariable = res).pack(side = TOP,expand = YES,fill = BOTH)

def show_result(cc,char):
    s = cc.get()
    if s == '0':
        s = ''
    s += char
    cc.set(s)

def ans(c, r):
    he = c.get()
    try:
        a = eval(he)
    except Exception:
        a = 'error'
    r.set(a)

def clear(c,r):
    c.set('')
    r.set('')

f1 = Frame(top)
bu1 = Button(f1, text = '1',fg = 'white', bg = 'blue',command = lambda c = cc ,o = '1':show_result(c, o),width =7, height = 2).pack(side = LEFT)
bu2 = Button(f1, text = '2',fg = 'white', bg = 'blue',command = lambda c = cc ,o = '2':show_result(c, o),width =7, height = 2).pack(side = LEFT)
bu3 = Button(f1, text = '3',fg = 'white', bg = 'blue',command = lambda c = cc ,o = '3':show_result(c, o),width =7, height = 2).pack(side = LEFT)
bu4 = Button(f1, text = '4',fg = 'white', bg = 'blue',command = lambda c = cc ,o = '4':show_result(c, o),width =7, height = 2).pack(side = LEFT)
buh = Button(f1, text = 'help',fg = 'white', bg = 'blue',command = None,width =7, height = 2).pack(side = LEFT)
f1.pack(side = TOP,expand = YES,fill = BOTH)
f2 = Frame(top)
bu5 = Button(f2, text = '5',fg = 'white', bg = 'blue',command = lambda c = cc ,o = '5':show_result(c, o),width =7, height = 2).pack(side = LEFT)
bu6 = Button(f2, text = '6',fg = 'white', bg = 'blue',command = lambda c = cc ,o = '6':show_result(c, o),width =7, height = 2).pack(side = LEFT)
bu7 = Button(f2, text = '7',fg = 'white', bg = 'blue',command = lambda c = cc ,o = '7':show_result(c, o),width =7, height = 2).pack(side = LEFT)
bu8 = Button(f2, text = '8',fg = 'white', bg = 'blue',command = lambda c = cc ,o = '8':show_result(c, o),width =7, height = 2).pack(side = LEFT)
f2.pack(side = TOP,expand = YES,fill = BOTH)
f3 = Frame(top)
bu9 = Button(f3, text = '9',fg = 'white', bg = 'blue',command = lambda c = cc ,o = '9':show_result(c, o),width =7, height = 2).pack(side = LEFT)
bu0 = Button(f2, text = '0',fg = 'white', bg = 'blue',command = lambda c = cc ,o = '0':show_result(c, o),width =7, height = 2).pack(side = LEFT)
bu11 = Button(f3, text = '+',fg = 'white', bg = 'blue',command = lambda c = cc ,o = '+':show_result(c, o),width =7, height = 2).pack(side = LEFT)
bu12 = Button(f3, text = '-',fg = 'white', bg = 'blue',command = lambda c = cc ,o = '-':show_result(c, o),width =7, height = 2).pack(side = LEFT)
bu13 = Button(f3, text = '*',fg = 'white', bg = 'blue',command = lambda c = cc ,o = '*':show_result(c, o),width =7, height = 2).pack(side = LEFT)
buh = Button(f3, text = '.',fg = 'white', bg = 'blue',command =lambda c = cc ,o = '.':show_result(c, o),width =7, height = 2).pack(side = LEFT)

f3.pack(side = TOP,expand = YES,fill = BOTH)
f4 = Frame(top)
bu15 = Button(f4, text = 'clear',fg = 'white', bg = 'blue',command = lambda c = cc, r = res :clear(c,r),width =7, height = 2).pack(side = LEFT)
bu16 = Button(f4, text = 'del',fg = 'white', bg = 'blue',command = lambda c = cc :c.set(c.get()[0:-1]),width =7, height = 2).pack(side = LEFT)
bu17 = Button(f4, text = 'quit',fg = 'white', bg = 'blue',command = top.quit,width =7, height = 2).pack(side = LEFT)
bu10 = Button(f4, text = '=',fg = 'white', bg = 'blue',command = lambda c = cc, r =res :ans(c, r),width =7, height = 2).pack(side = LEFT)
bu14 = Button(f4, text = '/',fg = 'white', bg = 'blue',command = lambda c = cc ,o = '/':show_result(c, o),width =7, height = 2).pack(side = LEFT)
f4.pack(side = TOP,expand = YES,fill = BOTH)

top.mainloop()

计算器2.0

#coding=utf8
from Tkinter import *
from functools import partial


top = Tk()

class cal(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.cc = '0'
        self.res = '0'
        self.master.title = '计算机2.0'
        self.master.geometry = '400x400'
        self.pack(expand = YES , fill = BOTH)
        self.dr()

    def add_button(self,p, side, text, command = None):
        b = Button(p, text = text, command = command, width =6, height =2)
        b.pack(side = side)
        return b

    def add_fr(self, p, side):
        f = Frame(p)
        f.pack(side = side,expand = YES,fill = BOTH)
        return f

    def show_re(self, a, b):
        c = a.get()
        s  = c + b
        a.set(s)

    def result(self, a, b):
        c= a.get()
        try:
            d = str(eval(c))
        except Exception:
            d = 'ERROR'
        b.set(d)
        return b

    def clear(self,a,b):
        a.set('')
        b.set('')

    def delt(self, a):
        b = a.get()[0:-1]
        a.set(b)

    def dr(self):
        cc = StringVar()
        res = StringVar()
        e1 = Entry(self, textvariable = cc )
        e1.pack(side = TOP,expand = YES, fill = BOTH)
        e2 = Entry(self, textvariable = res)
        e2.pack(side = TOP, expand = YES, fill = BOTH)
        b1 = Button(self, text = 'quit', command = top.quit, height = 2)
        b1.pack(side = BOTTOM,expand = YES, fill = BOTH)
        xixi = ('1234', '5678', '90-+', '*/.')
        for i in xixi:
            b = self.add_fr(self,TOP)
            for k in i:
                x = self.add_button(b, LEFT,text=k, command=lambda a = cc, b = k:self.show_re(a,b))
        b2 = self.add_button(b, TOP, text = '=', command = lambda a=cc, b=res:self.result(a,b) )
        b2.pack()
        f1 = self.add_fr(self,TOP)
        b3 = self.add_button(f1, LEFT, text='(',command=lambda a = cc, b = '(':self.show_re(a,b))
        b4 = self.add_button(f1, LEFT, text=')',command=lambda a = cc, b = ')':self.show_re(a,b))
        b5 = self.add_button(f1, LEFT, text='clear',command=lambda a = cc, b = res:self.clear(a,b))
        b6 = self.add_button(f1, LEFT, text='del',command=lambda a = cc:self.delt(a))
def main():
    cal().mainloop()

if __name__ == '__main__':
    main()

写的好丑,。。。。不忍直视

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值