[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()


 
时间: 2024-08-10 10:39:59

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

值得学习的python项目

此网站整理的2014年值得一学的pytho项目,http://pycoders.com/2014/ 项目资源都在github上,python程序员进阶必备. 就当作2015年的计划吧.先列表,再对号 Projects 1. robobrowser A library for web scraping built on Requests and BeautifulSoup. Like Mechanize, but with tests, docs, and a Pythonic interface

python项目练习地址

作者:Wayne Shi链接:http://www.zhihu.com/question/29372574/answer/88744491来源:知乎著作权归作者所有,转载请联系作者获得授权. 目前是34个Python项目,会继续保持更新.Learn by doing才是正确的技术学习姿势.20160816更新:Python - 高德API+Python解决租房问题Python - 基于 Flask 及爬虫实现微信娱乐机器人Python - Python3 实现淘女郎照片爬虫Python - Py

怎么使用pipenv管理你的python项目

原文链接:https://robots.thoughtbot.com/how-to-manage-your-python-projects-with-pipenv 翻译者:Jiong 在thoughtbot,我们用Ruby和Rails工作,但通常我们总是尝试使用最合适的语言或者框架来解决问题.我最近一直在探索机器学习技术,所以Python使用地更多. Ruby项目和Python项目处理之间的一个很大的区别就是管理依赖关系方式的不同.目前在Python语言中没有类似于Bundler或Gemfile

Python项目使用memcached缓存

前言许多Web应用都将数据保存到MySQL这样的关系型数据库管理系统中,应用服务器从中读取数据并在浏览器中显示. 但随着数据量的增大.访问的集中,就会出现数据库的负担加重.数据库响应恶化. 网站显示延迟等不良影响.分布式缓存是优化网站性能的重要手段,大量站点都通过可伸缩的服务器集群提供大规模热点数据缓存服务.通过缓存数据库查询结果,减少数据库访问次数,可以显著提高动态Web应用的速度和可扩展性.业界常用的有redis.memcached等,今天要讲的就是在python项目中如何使用memcach

适合初学者学习的Python书籍列表(2018年最新书籍)

随着人工智能和机器学习的发展,各行各业职位正在逐渐被智能机器取替,为了应对被取替的风险,越来越多人开始学习编程,最近一年,似乎要进入全民Python编程的时代了.那么,为什么要学习Python?Python已经逐渐成为大众必备的一门技能.回想一下,微软创始人比尔·盖茨 13 岁学习编程,Facebook 创始人扎克伯格 11 岁开始学习编程.时代发展日新月异,小学生学编程早已不是新鲜事,如果说 2017 年有一门最火"外语",那一定是 Python.现在学习Python,已经不只是程序

Jenkins部署Python项目实战

一.背景 我们工作中常用Jenkins部署Java代码,因其灵活的插件特性,例如jdk,maven,ant等使得java项目编译后上线部署一气呵成,同样对于脚本语言类型如Python上线部署,利用Jenkins强大的插件功能,轻松实现CI/CD,但如果部署多项目到同一台服务器涉及环境一致性问题,对此可以利用容器技术Docker解决,也可以利用Python虚拟环境例如virutalenv或conda等优秀等工具解决,在此由于后期根据requirements来安装依赖包比较慢,且后期需要将Pytho

Python学习教程(Python学习路线+Python学习视频):Python数据结构

Python学习教程(Python学习路线+Python学习视频):Python数据结构   数据结构引言:   数据结构是组织数据的方式,以便能够更好的存储和获取数据.数据结构定义数据之间的关系和对这些数据的操作方式.数据结构屏蔽了数据存储和操作的细节,让程序员能更好的处理业务逻辑,同时拥有快速的数据存储和获取方式. 在这篇文章中,你将了解到多种数据结构以及这些数据结构在Python中实现的方式.    抽象数据类型和数据结构 数据结构是抽象数据类型(ADT)的实现,通常,是通过编程语言提供的

学习《Python机器学习(第2版)》中文PDF+英文PDF+代码分析+Sebastian

学习数据科学的初学者,想进一步拓展对数据科学领域的认知,推荐学习<Python机器学习(第二版)>.<Python机器学习(第二版)>将机器学习背后的基本理论与应用实践联系起来,聚焦于如何正确地提出问题.解决问题,能帮助了解如何使用Python解决数据中的关键问题. <Python机器学习(第2版)>介绍如何用Python和基于Python的机器学习软件库进行实践,对机器学习概念的必要细节进行讨论,同时对机器学习算法的工作原理.使用方法以及如何避免掉入常见的陷阱提供直观

爬虫学习 04.Python网络爬虫之requests模块(1)

爬虫学习 04.Python网络爬虫之requests模块(1) 引入 Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用. 警告:非专业使用其他 HTTP 库会导致危险的副作用,包括:安全缺陷症.冗余代码症.重新发明轮子症.啃文档症.抑郁.头疼.甚至死亡. 今日概要 基于requests的get请求 基于requests模块的post请求 基于requests模块ajax的get请求 基于requests模块ajax的post请求 综合项目练习:爬取国家药品监