Python Tkinter实现简易计算器功能


Posted in Python onJanuary 30, 2018

闲暇时间用tkinter写了个简易计算器,可实现简单的加减乘除运算,用了Button和Entry2个控件,下面是代码,只是简单的用了偏函数partial,因为那么多button的大部分参数都是一样的,使用偏函数可以简化参数传递,避免同样的参数传递写N次。

# -*- coding: utf-8 -*- 
#author: Cullen 
 
#import the module 
from Tkinter import * 
import 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()

下面是运行结果:

Python Tkinter实现简易计算器功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中内置数据类型list,tuple,dict,set的区别和用法
Dec 14 Python
简单了解Python中的几种函数
Nov 03 Python
Python使用matplotlib简单绘图示例
Feb 01 Python
python实现对指定输入的字符串逆序输出的6种方法
Apr 26 Python
Python爬虫包BeautifulSoup学习实例(五)
Jun 17 Python
python 平衡二叉树实现代码示例
Jul 07 Python
python单例模式实例解析
Aug 28 Python
一百多行python代码实现抢票助手
Sep 25 Python
python 判断字符串中是否含有汉字或非汉字的实例
Jul 15 Python
python中id函数运行方式
Jul 03 Python
linux系统下pip升级报错的解决方法
Jan 31 Python
python基于turtle绘制几何图形
Jun 15 Python
python使用tkinter实现简单计算器
Jan 30 #Python
Python实现简单遗传算法(SGA)
Jan 29 #Python
Python之reload流程实例代码解析
Jan 29 #Python
Python中的默认参数实例分析
Jan 29 #Python
Python使用遗传算法解决最大流问题
Jan 29 #Python
Python subprocess模块详细解读
Jan 29 #Python
python微信跳一跳游戏辅助代码解析
Jan 29 #Python
You might like
实用函数5
2007/11/08 PHP
php时间不正确的解决方法
2008/04/09 PHP
使用php判断网页是否gzip压缩
2013/06/25 PHP
浅析php单例模式
2014/11/25 PHP
PHP、Python和Javascript的装饰器模式对比
2015/02/03 PHP
PhpStorm 2020.3:新增开箱即用的PHP 8属性(推荐)
2020/10/30 PHP
JavaScript实现的日期控件具体代码
2013/11/18 Javascript
jquery遍历筛选数组的几种方法和遍历解析json对象
2013/12/13 Javascript
javascript动态添加、修改、删除对象的属性与方法详解
2014/01/27 Javascript
jquery网页回到顶部效果(图标渐隐,自写)
2014/06/16 Javascript
Ajax局部更新导致JS事件重复触发问题的解决方法
2014/10/14 Javascript
js检测iframe是否加载完成的方法
2015/11/26 Javascript
javascript html5 canvas实现可拖动省份的中国地图
2016/03/11 Javascript
html+js+highcharts绘制圆饼图表的简单实例
2016/08/04 Javascript
jQuery根据ID、CLASS、等获取对象的实例
2016/12/04 Javascript
微信小程序实现图片自适应(支持多图)
2017/01/25 Javascript
完美解决axios在ie下的兼容性问题
2018/03/05 Javascript
Angular动画实现的2种方式以及添加购物车动画实例代码
2018/08/09 Javascript
js操作table中tr的顺序实现上移下移一行的效果
2018/11/22 Javascript
对Layer UI 模块化的用法详解
2019/09/26 Javascript
vue项目打包为APP,静态资源正常显示,但API请求不到数据的操作
2020/09/12 Javascript
[48:21]Mski vs VGJ.S Supermajor小组赛C组 BO3 第一场 6.3
2018/06/04 DOTA
python中lambda函数 list comprehension 和 zip函数使用指南
2014/09/28 Python
使用pandas批量处理矢量化字符串的实例讲解
2018/07/10 Python
Python collections中的双向队列deque简单介绍详解
2019/11/04 Python
利用Python中的Xpath实现一个在线汇率转换器
2020/09/09 Python
HTML5 WebSocket实现点对点聊天的示例代码
2018/01/31 HTML / CSS
Java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类
2012/02/06 面试题
工程造价专业大学生自荐信
2013/10/01 职场文书
工程业务员岗位职责
2013/12/31 职场文书
初级会计求职信范文
2014/02/15 职场文书
体育教学随笔感言
2014/02/24 职场文书
酒店开业庆典策划方案
2014/05/28 职场文书
教育实践活动对照检查材料
2014/09/23 职场文书
英文辞职信范文
2015/05/13 职场文书
黄埔军校观后感
2015/06/10 职场文书