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函数返回多个值的示例方法
Dec 04 Python
python关闭windows进程的方法
Apr 18 Python
简单总结Python中序列与字典的相同和不同之处
Jan 19 Python
判断网页编码的方法python版
Aug 12 Python
Python图片裁剪实例代码(如头像裁剪)
Jun 21 Python
Python实现控制台中的进度条功能代码
Dec 22 Python
使用PM2+nginx部署python项目的方法示例
Nov 07 Python
anaconda如何查看并管理python环境
Jul 05 Python
让Python脚本暂停执行的几种方法(小结)
Jul 11 Python
python字典的值可以修改吗
Jun 29 Python
python PIL模块的基本使用
Sep 29 Python
Python标准库pathlib操作目录和文件
Nov 20 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
php+ajax实现无刷新的新闻留言系统
2020/12/21 PHP
支付宝支付开发――当面付条码支付和扫码支付实例
2016/11/04 PHP
php生成0~1随机小数的方法(必看)
2017/04/05 PHP
PHP实现的数独求解问题示例
2017/04/18 PHP
yii gridview实现时间段筛选功能
2017/08/15 PHP
Laravel 微信小程序后端搭建步骤详解
2019/11/26 PHP
[原创]图片分页查看
2006/08/28 Javascript
qTip 基于JQuery的Tooltip插件[兼容性好]
2010/09/01 Javascript
Javascript事件热键兼容ie|firefox
2010/12/30 Javascript
js判断IE6/IE7/FF的代码[XMLHttpRequest]
2011/02/16 Javascript
node.js入门教程
2014/06/01 Javascript
angularJs关于指令的一些冷门属性详解
2016/10/24 Javascript
用move.js库实现百叶窗特效
2017/02/08 Javascript
js基于myFocus实现轮播图效果
2017/02/14 Javascript
Angularjs2不同组件间的通信实例代码
2017/05/06 Javascript
浅谈箭头函数写法在ReactJs中的使用
2017/08/22 Javascript
实例教学如何写vue插件
2017/11/30 Javascript
Ant-design-vue Table组件customRow属性的使用说明
2020/10/28 Javascript
[55:35]DOTA2-DPC中国联赛 正赛 CDEC vs Dragon BO3 第二场 1月22日
2021/03/11 DOTA
Python使用htpasswd实现基本认证授权的例子
2014/06/10 Python
python里将list中元素依次向前移动一位
2014/09/12 Python
在服务器端实现无间断部署Python应用的教程
2015/04/16 Python
python基础教程之匿名函数lambda
2017/01/17 Python
Python实现的redis分布式锁功能示例
2018/05/29 Python
基于Python pip用国内镜像下载的方法
2018/06/12 Python
基于随机梯度下降的矩阵分解推荐算法(python)
2018/08/31 Python
python爬虫的一个常见简单js反爬详解
2019/07/09 Python
Django中自定义admin Xadmin的实现代码
2019/08/09 Python
Python SELENIUM上传文件或图片实现过程
2019/10/28 Python
Python注释、分支结构、循环结构、伪“选择结构”用法实例分析
2020/01/09 Python
解决virtualenv -p python3 venv报错的问题
2021/02/05 Python
以实惠的价格提供高品质的时尚:Newchic
2018/01/18 全球购物
领导证婚人证婚词
2014/01/13 职场文书
中学教师请假制度
2014/02/03 职场文书
保安队长职务说明书
2014/02/23 职场文书
党的生日活动方案
2014/08/15 职场文书