python简单计算器
来源:互联网 发布:关闭80端口会怎么样 编辑:程序博客网 时间:2024/06/06 11:38
计算器1.0
#coding=utf8from Tkinter import *from functools import partial as pttop = 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=utf8from Tkinter import *from functools import partialtop = 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()
写的好丑,。。。。不忍直视
0 0
- Python简单计算器
- python简单计算器
- Python简单计算器
- Python 简单的计算器
- Python 简单计算器实现
- Python实现的简单计算器
- 简单scheme计算器-python实现
- python最简单的计算器
- python tkinter实现简单计算器
- 简单实用的Python计算器
- Python: python的简单表达式计算器
- 一个简单的计算器的python程序
- python tkinter 实现简单计算器2
- Python Tkinter实现的简单计算器
- Python制作一个简单的计算器
- 简单计算器
- 简单计算器
- 简单计算器
- epoll源码实现分析[整理]
- 基于PhoneGap的Android应用开发:Get started
- 从陌陌淡化社交说起
- 欢迎使用CSDN-markdown编辑器
- java.lang.ClassNotFoundException: Didn't find class "com.kge.love.base.BaseApplication" on path: Dex
- python简单计算器
- android 中在activity弹出一个对话框,并不会执行onpause生命周期方法
- 现在 未来 的趋势 大屏幕拼接控制器 网络分布式处理器--广州中淦信息科技
- Wallflower paper测试数据集(原Test Images for Wallflower Paper)
- 解决 iOS 7 UIBarButtonItem 在 navigationBar 中移位问题
- 大数据相关术语(2)
- java对图片进行“高保真”压缩
- cdn和dns
- sql server 2008R2修改sa密码的简单方式