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遍历C盘dll文件的方法
May 06 Python
Python实现简单截取中文字符串的方法
Jun 15 Python
如何使用python爬取csdn博客访问量
Feb 14 Python
Python的Flask框架应用调用Redis队列数据的方法
Jun 06 Python
python安装oracle扩展及数据库连接方法
Feb 21 Python
OpenCV2从摄像头获取帧并写入视频文件的方法
Aug 03 Python
pandas.DataFrame的pivot()和unstack()实现行转列
Jul 06 Python
浅析python 中大括号中括号小括号的区分
Jul 29 Python
Tensorflow 多线程设置方式
Feb 06 Python
记一次python 爬虫爬取深圳租房信息的过程及遇到的问题
Nov 24 Python
Pycharm配置autopep8实现流程解析
Nov 28 Python
python tkinter实现定时关机
Apr 21 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
UTF8编码内的繁简转换的PHP类
2009/07/09 PHP
PHP面向对象程序设计模拟一般面向对象语言中的方法重载(overload)示例
2019/06/13 PHP
js常见表单应用技巧
2008/01/09 Javascript
JS拖动技术 关于setCapture使用
2010/12/09 Javascript
修复IE9&amp;safari 的sort方法
2011/10/21 Javascript
jquery选择器之属性过滤选择器详解
2014/01/27 Javascript
JS判断是否在微信浏览器打开的简单实例(推荐)
2016/08/24 Javascript
jQuery版AJAX简易封装代码
2016/09/14 Javascript
分享javascript、jquery实用代码段
2016/10/20 Javascript
canvas实现粒子时钟效果
2017/02/06 Javascript
node打造微信个人号机器人的方法示例
2018/04/26 Javascript
vue-router中scrollBehavior的巧妙用法
2018/07/09 Javascript
详解a标签添加onclick事件的几种方式
2019/03/29 Javascript
解决layui弹框失效的问题
2019/09/09 Javascript
js实现数字从零慢慢增加到指定数字示例
2019/11/07 Javascript
VUE异步更新DOM - 用$nextTick解决DOM视图的问题
2020/11/06 Javascript
[02:16]深扒TI7聊天轮盘语音出处2
2017/05/11 DOTA
[55:44]OG vs NAVI 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
[46:49]完美世界DOTA2联赛PWL S3 access vs Rebirth 第二场 12.19
2020/12/24 DOTA
Python列表切片用法示例
2017/04/19 Python
TF-IDF与余弦相似性的应用(一) 自动提取关键词
2017/12/21 Python
python机器学习理论与实战(四)逻辑回归
2018/01/19 Python
pygame实现俄罗斯方块游戏(基础篇1)
2019/10/29 Python
pytorch中的transforms模块实例详解
2019/12/31 Python
python3代码中实现加法重载的实例
2020/12/03 Python
美国顶级防滑鞋:Shoes For Crews
2017/03/27 全球购物
英国休闲奢华的缩影:Crew Clothing
2019/05/05 全球购物
李维斯牛仔裤荷兰官方网站:Levi’s NL
2020/08/23 全球购物
史上最全面的Java面试题汇总!
2015/02/03 面试题
学习心得体会
2014/01/01 职场文书
少先队学雷锋活动总结范文
2014/03/09 职场文书
安全宣传标语口号
2014/06/06 职场文书
起诉离婚协议书样本
2014/11/25 职场文书
详解Redis实现限流的三种方式
2021/04/27 Redis
使用JS实现简易计算器
2021/06/14 Javascript
ORACLE中dbms_output.put_line输出问题的解决过程
2022/06/28 Oracle