zoukankan      html  css  js  c++  java
  • [IT学习]转载python 项目 计算器

    这个是从网上搜到的Python小项目之计算器(原文地址:http://www.2cto.com/kf/201402/279637.html)。但该段代码估计是Python 2 写的。

    如果你使用的程序是Python 3以上版本,需要自行安装pillow。Windows可以在命令行中使用pip install pillow。

    但是在程序中引用时,pillow的名称依然是PIL。具体看下面的例子。

    【思考】

    1、tkinter 编程要考虑哪些方面?

    2、这个程序还有哪些不完善的?例如输入9+-2,看看结果是多少?

    3、把电脑里的计算器拿出来看看,对比下,有哪些地方可以完善?

    4、grid方式布局有哪些优点,有哪些缺点?

    # -*- coding: utf-8 -*-
    #author: Cullen
    #原来的代码中有这个作者信息,可以以“cullent python”搜搜看,不确定是否同一个人
    #import the module
    from tkinter import *
    import tkinter.font as tkFont
    import os
    from functools import partial
    from PIL import Image, ImageTk
     
    def get_input(entry, argu):
        entry.insert(END, argu)
     
    def backspace(entry):
        input_len = len(entry.get())
        entry.delete(input_len - 1)
     
    def clear(entry):
        entry.delete(0, END)
     
    def calc(entry):
        input = entry.get()
        output = str(eval(input.strip()))
        clear(entry)
        entry.insert(END, output)
     
    def cal():
        root = Tk()
        root.title("Calc")
        root.resizable(0,0)
     
        entry_font = tkFont.Font(size=12)
        entry = Entry(root, justify="right", font=entry_font)
        entry.grid(row=0, column=0, columnspan=4, sticky=N+W+S+E, padx=5,  pady=5)
     
        button_font = tkFont.Font(size=10, weight=tkFont.BOLD)
        button_bg = '#D5E0EE'
        button_active_bg = '#E5E35B'
     
        myButton = partial(Button, root, bg=button_bg, padx=10, pady=3, activebackground = button_active_bg)
     
        button7 = myButton(text='7', command=lambda : get_input(entry, '7'))
        button7.grid(row=1, column=0, pady=5)
     
        button8 = myButton(text='8', command=lambda : get_input(entry, '8'))
        button8.grid(row=1, column=1, pady=5)
     
        button9 = myButton(text='9', command=lambda : get_input(entry, '9'))
        button9.grid(row=1, column=2, pady=5)
     
        button10 = myButton(text='+', command=lambda : get_input(entry, '+'))
        button10.grid(row=1, column=3, pady=5)
     
        button4 = myButton(text='4', command=lambda : get_input(entry, '4'))
        button4.grid(row=2, column=0, pady=5)
     
        button5 = myButton(text='5', command=lambda : get_input(entry, '5'))
        button5.grid(row=2, column=1, pady=5)
     
        button6 = myButton(text='6', command=lambda : get_input(entry, '6'))
        button6.grid(row=2, column=2, pady=5)
     
        button11 = myButton(text='-', command=lambda : get_input(entry, '-'))
        button11.grid(row=2, column=3, pady=5)
     
        button1 = myButton(text='1', command=lambda : get_input(entry, '1'))
        button1.grid(row=3, column=0, pady=5)
     
        button2 = myButton(text='2', command=lambda : get_input(entry, '2'))
        button2.grid(row=3, column=1, pady=5)
     
        button3 = myButton(text='3', command=lambda : get_input(entry, '3'))
        button3.grid(row=3, column=2, pady=5)
     
        button12 = myButton(text='*', command=lambda : get_input(entry, '*'))
        button12.grid(row=3, column=3, pady=5)
     
        button0 = myButton(text='0', command=lambda : get_input(entry, '0'))
        button0.grid(row=4, column=0, columnspan=2, padx=3, pady=5, sticky=N+S+E+W)
     
        button13 = myButton(text='.', command=lambda : get_input(entry, '.'))
        button13.grid(row=4, column=2, pady=5)
     
        button14 = Button(root, text='/', bg=button_bg, padx=10, pady=3,
                          command=lambda : get_input(entry, '/'))
        button14.grid(row=4, column=3, pady=5)
     
        button15 = Button(root, text='<-', bg=button_bg, padx=10, pady=3,
                          command=lambda : backspace(entry), activebackground = button_active_bg)
        button15.grid(row=5, column=0, pady=5)
     
        button16 = Button(root, text='C', bg=button_bg, padx=10, pady=3,
                          command=lambda : clear(entry), activebackground = button_active_bg)
        button16.grid(row=5, column=1, pady=5)
     
        button17 = Button(root, text='=', bg=button_bg, padx=10, pady=3,
                          command=lambda : calc(entry), activebackground = button_active_bg)
        button17.grid(row=5, column=2, columnspan=2, padx=3, pady=5, sticky=N+S+E+W)
     
        root.mainloop()
     
    if __name__ == '__main__':
        cal()
    
    
    
    
     
  • 相关阅读:
    可爱的中国电信 请问我们的电脑还属于我们自己吗?
    了解客户的需求,写出的代码或许才是最优秀的............
    DELPHI DATASNAP 入门操作(3)简单的主从表的简单更新【含简单事务处理】
    用数组公式获取字符在字符串中最后出现的位置
    在ehlib的DBGridEh控件中使用过滤功能(可以不用 MemTableEh 控件 适用ehlib 5.2 ehlib 5.3)
    格式化json返回的时间
    ExtJs中使用Ajax赋值给全局变量异常解决方案
    java compiler level does not match the version of the installed java project facet (转)
    收集的资料(六)ASP.NET编程中的十大技巧
    收集的资料共享出来(五)Asp.Net 权限解决办法
  • 原文地址:https://www.cnblogs.com/viphhs/p/7256042.html
Copyright © 2011-2022 走看看