simple calculator

最近,闲得蛋疼,画个计算器,大牛勿喷~

source like this

#-*- coding:UTF-8-*-
 
from Tkinter import *
 
def frame(root, side):
    w = Frame(root)
    w.pack(side=side, expand=YES, fill=BOTH)
    return w
#end of def
 
def button(root, side, text, command=None):
    w = Button(root, text=text, command=command)
    w.pack(side=side, expand=YES, fill=BOTH)
    return w
#end of def
 
class Calculator(Frame):
    def __init__(self):
        Frame.__init__(self)
        #self.oprion_add(‘*Font‘, ‘Verdana 12 bold‘)
        self.option_add(‘*Font‘, ‘Verdana 12 bold‘)
        self.pack(expand=YES, fill=BOTH)
        self.master.title(‘simple calculator‘)
        self.master.iconname(‘calc1‘)
         
        display = StringVar()
        Entry(self, relief=SUNKEN, textvariable=display).pack(side=TOP, expand=YES, fill=BOTH)
        for key in (‘123‘, ‘456‘, ‘789‘, ‘+0.‘):
            keyF = frame(self, TOP)
            for char in key:
                button(keyF, LEFT, char, lambda w=display, c=char: w.set(w.get() + c))
        #end of for
        opsF = frame(self, TOP)
         
        for char in ‘-*/=‘:
            if char == ‘=‘:
                btn = button(opsF, LEFT, char)
                btn.bind(‘<ButtonRelease-1>‘, lambda e, s=self, w=display: s.calc(w), ‘+‘)
            else:
                btn = button(opsF, LEFT, char, lambda w=display, s=‘%s ‘%char: w.set(w.get() + s))
 
            #end of if
        #end of for
        clearF = frame(self, BOTTOM)
        button(clearF, LEFT, ‘Clr‘, lambda w=display: w.set(‘‘))
    #end of def
     
    def calc(self, display):
        try:
            display.set(eval(display.get()))
        except:
            display.set(‘ERROR‘)
        #end of try and except
    #end of def
#end of class
 
if __name__ == ‘__main__‘:
    Calculator().mainloop()

running like this

时间: 2024-10-05 21:02:05

simple calculator的相关文章

ios 学习之 Simple Calculator Application

// // ViewController.h // ocTest // // Created by Hu Li on 2018/12/30. // Copyright ? 2018 Hu Li. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController{ bool operatorPressed; bool add; NSString *firstEntry; NSS

Permissible Privileges for GRANT and REVOKE

Table 6.2 Permissible Privileges for GRANT and REVOKE Privilege Column Context ALL [PRIVILEGES] Synonym for "all privileges" Server administration ALTER Alter_priv Tables ALTER ROUTINE Alter_routine_priv Stored routines CREATE Create_priv Databa

MySQL入门手册

本文内容摘自MySQL5.6官方文档,主要选取了在实践过程中所用到的部分文字解释,力求只摘录重点,快速学会使用MySQL,本文所贴代码地方就是我亲自练习过的代码,凡本文没有练习过的代码都没有贴在此处,如果读者想自己尝试,可以查看官方文档,文中给出了原官方文档的对应链接以供查阅. 本文地址:http://www.cnblogs.com/yhLinux/p/4019386.html http://dev.mysql.com/doc/refman/5.6/en/tutorial.htmlThis ch

MySQL Keynote

[MySQL Keynote] 1.Keywords may be entered in any lettercase. The following queries are equivalent: 2.Here is another query. It demonstrates that you can use mysql as a simple calculator: 3.所有查询以;结尾. 4.显示数据库. SHOW DATABASES does not show databases tha

【转】编写Chrome扩展程序

Chrome的扩展程序很多,也很容易入门,可以来简单实现一下 看看 官方文档 或者翻译的文档:百度.360,慢慢就能实现出一个扩展程序来 每个扩展程序应用一般会包含: 一个manifest清单文件 html文件 js文件 其他文件等 可以看到,其实结构如同一般的页面,有共通之处. 一.了解Chrome扩展程序 Chrome扩展程序商店地址为:https://chrome.google.com/webstore/category/extensions?hl=zh-CN 访问Chrome浏览器中已安

留念 C语言第一课简单的计算器制作

学C语言这么久了.  /* 留念 C语言第一课简单的计算器制作 */ #include<stdio.h>  #include<stdlib.h> #include<conio.h>void displayMenu();void add();void sub();void multiply();void divide();void yushu();void jiecheng();void leijia();void jiechengleijia();int main(in

C# final project

Problem Statement You are tasked with developing a task manager. The task manager will allow people to add a new task, modify or delete an existing task. Each task must have a title, a due date and a description. The user interface should be simple,

非正式介绍Python(一)

3.1. Using Python as a Calculator Let’s try some simple Python commands. Start the interpreter and wait for the primary prompt, >>>. (It shouldn’t take long.) 我们先试着写些简单的Python命令语句,打开Python解释器(IDLE(后面出现解释器一般指IDLE)),等待>>>提示符的出现. 3.1.1. Num

【python】An Introduction to Interactive Programming in Python(week two)

This is a note for https://class.coursera.org/interactivepython-005 In week two, I have learned: 1.event-drvien programing 4 event types: Input: button, textbox Keyborad: key up, key down Mouse: click, drag Timer example: # Example of a simple event-