这个是从网上搜到的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()


 

[IT学习]转载python 项目 计算器的更多相关文章

  1. 精选 TOP45 值得学习的Python项目

    精选 TOP45 值得学习的Python项目 [导读]热门资源博客 Mybridge AI 比较了 18000 个关于 Python 的项目,并从中精选出 45 个最具竞争力的项目.我们进行了翻译,在 ...

  2. Python学习教程(十)精选 TOP45 值得学习的Python项目

    精选 TOP45 值得学习的Python项目 [导读]热门资源博客 Mybridge AI 比较了 18000 个关于 Python 的项目,并从中精选出 45 个最具竞争力的项目.我们进行了翻译,在 ...

  3. 从零开始学习PYTHON3讲义(二)把Python当做计算器

    <从零开始PYTHON3>第二讲 上一讲我们说过了如何启动Python IDLE集成开发学习环境,macOS/Linux都可以在命令行执行idle3.Windows则从开始菜单中去寻找ID ...

  4. python项目推荐(转载知乎)

    作者:Wayne Shi链接:https://www.zhihu.com/question/29372574/answer/88744491来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商 ...

  5. IDEA 学习笔记之 Python项目开发

    Python项目开发: 下载Python: https://www.python.org/downloads/release/python-363/ 安装Python: 配置环境变量(path): C ...

  6. [转载]Python兵器谱

    转载自:http://www.52nlp.cn/python-网页爬虫-文本处理-科学计算-机器学习-数据挖掘 曾经因为NLTK的缘故开始学习Python,之后渐渐成为我工作中的第一辅助脚本语言,虽然 ...

  7. python项目练习地址

    作者:Wayne Shi链接:http://www.zhihu.com/question/29372574/answer/88744491来源:知乎著作权归作者所有,转载请联系作者获得授权. 目前是3 ...

  8. python学习: 优秀Python学习资源收集汇总--转

    Python是一种面向对象.直译式计算机程序设计语言.它的语法简捷和清晰,尽量使用无异义的英语单词,与其它大多数程序设计语言使用大括号不一样,它使用縮进来定义语句块.与Scheme.Ruby.Perl ...

  9. python项目内import其他内部package的模块的正确方法

    转载 :https://blog.csdn.net/u011089523/article/details/52931844 本文主要介绍如何在一个Python项目中,优雅的实现项目内各个package ...

随机推荐

  1. swoole 编程环境安装

    安装linux debian 9.8 购买阿里云vps web目录规划 lnmp/source lnmp/soft lnmp/data 源码安装php apt install gcc make apt ...

  2. mysql查最大字符串

    select MAX(comp_code+0) from t_base_company 字符串 +0 把字符串转成数字

  3. Leetcode 224.基本计算器

    基本计算器 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式可以包含左括号 ( ,右括号 ),加号 + ,减号 -,非负整数和空格  . 示例 1: 输入: "1 + 1 ...

  4. Go map例题

    package main import "fmt" //map例题 //寻找最长不含有重复字符的子串 // abcabcbb -> abc //pwwkew ->wke ...

  5. Poj3253:Fence Repair 【贪心 堆】

    题目大意:背景大概是个资本家剥削工人剩余价值的故事....有一块木板,要把它切成几个长度,切一次的费用是这整块被切木板的长度,例如将一个长度为21的木板切成2和19两块费用为21,切成两块的长度及顺序 ...

  6. LibreOJ2302 - 「NOI2017」整数

    Portal Description 有一个整数\(x=0\),对其进行\(n(n\leq10^6)\)次操作: 给出\(a(|a|\leq10^9),b(b\leq30n)\),将\(x\)加上\( ...

  7. 洛谷P2058 海港

    题目描述 小K是一个海港的海关工作人员,每天都有许多船只到达海港,船上通常有很多来自不同国家的乘客. 小K对这些到达海港的船只非常感兴趣,他按照时间记录下了到达海港的每一艘船只情况:对于第i艘到达的船 ...

  8. 【HDOJ6333】Harvest of Apples(莫队)

    题意: 给定T组询问,每组有两个数字n和m,求sigma i=0..m c(n,i) 答案对1e9+7取模 T<=1e5 1<=n,m<=1e5 思路: 注意要先变n再变m,否则会因 ...

  9. C++动态特性和C++对象模型——《高质量程序设计12章》

    1.动态特性 静态特性和动态特性,编译时和运行时 虚函数 (1)虚函数的叫覆盖,虚函数不是实现多态的唯一手段(其他语言也可能采用别的方法). 抽象基类: (1)如果将基类的虚函数声明为纯虚函数,则基类 ...

  10. PatentTips - Reducing Write Amplification in a Flash Memory

    BACKGROUND OF THE INVENTION Conventional NAND Flash memories move data in the background to write ov ...