python 计算器
2015-07-13 14:38
691 查看
#!/usr/bin/python2.6 # -*- coding: utf-8 -*- from Tkinter import * #root = Tk() # 80x80代表了初始化时主窗口的大小,0,0代表了初始化时窗口所在的位置 #root.geometry('800x600+10+10') #root.mainloop() class yyCalcutor(Frame): def __init__(self): Frame.__init__(self) self.pack(expand = YES, fill = BOTH) self.master.title('Simple Calculater') display = StringVar() Entry(self, relief = SUNKEN,textvariable = display).pack(side = TOP, expand = YES,fill = BOTH) #当expand选项为”yes”时,填充父组件的剩余空间 for key in('123', '456', '789', '-0.'): keyF = self.frame(self, TOP) for char in key: self.button(keyF, LEFT, char, lambda w = display, c = char:w.set(w.get() + c)) opsF = self.frame(self, TOP) for char in '+-*/=': if char == '=': btn = self.button(opsF, LEFT, char) print btn btn.bind('<ButtonRelease - 1>', lambda e, s = self, w = display:s.calc(w), '+') else: btn = self.button(opsF, LEFT, char, lambda w = display, s = '%s' %char:w.set(w.get() + s)) clearF = self.frame(self, BOTTOM) self.button(clearF, LEFT, 'clear', lambda w = display:w.set('')) def button(self,root, side, text, command = None): w = Button(root, text = text, command = command) w.pack(side = side, expand = YES, fill = BOTH) #return w def frame(self,root, side): w = Frame(root) w.pack(side = side, expand = YES, fill = BOTH) return w #假如你有一个类称为MyClass和这个类的一个实例MyObject。当你调用这个对象的方法MyObject.method(arg1, arg2)的时候,这会由Python自动转为MyClass.method(MyObject, arg1,arg2)——这就是self的原理了 if __name__=='__main__': yyCalcutor().mainloop()
相关文章推荐
- Python.Paste指南之Deploy
- python 读取文件
- python 函数参数
- Python3.2官方文件翻译-工具列表和十进制浮点计算
- python第一个爬虫
- python第三方库系列之十八--python/django test库
- Python三种结构
- 【Python 学习手册笔记】动态类型简介
- strategy pattern in Python
- Python: Unable to find vcvarsall.bat 解决方法
- python 加密解密
- Python学习笔记
- Notepad++调试运行Python脚本方法
- python加载图片
- python inconsistent dedent(不一致的缩进)
- python编译as
- python入门读书笔记
- 第一个Python小程序
- python 中得 “=”
- Python+Numpy+Scipy+Matplotlib+IPython(一)