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列表删除的三种方法代码分享
Oct 31 Python
详解Python nose单元测试框架的安装与使用
Dec 20 Python
Django后台获取前端post上传的文件方法
May 28 Python
pyshp创建shp点文件的方法
Dec 31 Python
一文了解Python并发编程的工程实现方法
May 31 Python
Python正则表达式学习小例子
Mar 03 Python
python使用ctypes库调用DLL动态链接库
Oct 22 Python
selenium学习教程之定位以及切换frame(iframe)
Jan 04 Python
Django如何重置migration的几种情景
Feb 24 Python
在pycharm中无法import所安装的库解决方案
May 31 Python
Pygame Draw绘图函数的具体使用
Nov 17 Python
Python 操作pdf pdfplumber读取PDF写入Exce
Aug 14 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实现将GB编码转换为UTF8
2006/11/25 PHP
php生成略缩图代码
2012/07/16 PHP
php 使用file_get_contents读取大文件的方法
2014/11/13 PHP
新浪SAE搭建PHP项目教程
2015/01/28 PHP
深入理解PHP中的Streams工具
2015/07/03 PHP
Prototype源码浅析 Enumerable部分(二)
2012/01/18 Javascript
JS上传图片前的限制包括(jpg jpg gif及大小高宽)等
2012/12/19 Javascript
jQuery使用技巧简单汇总
2013/04/18 Javascript
Jquery中children与find之间的区别详细解析
2013/11/29 Javascript
Javascript 是你的高阶函数(高级应用)
2015/06/15 Javascript
js clearInterval()方法的定义和用法
2015/11/11 Javascript
Jqgrid之强大的表格插件应用
2015/12/02 Javascript
很棒的js选项卡切换效果
2016/07/15 Javascript
基于Node.js的WebSocket通信实现
2017/03/11 Javascript
在node中如何使用 ES6
2017/04/22 Javascript
20行js代码实现的贪吃蛇小游戏
2017/06/20 Javascript
基于rollup的组件库打包体积优化小结
2018/06/18 Javascript
微信小程序动态评分展示/五角星展示/半颗星展示/自定义长度展示功能的实现
2020/07/22 Javascript
如何基于viewport vm适配移动端页面
2020/11/13 Javascript
[20:21]《一刀刀一天》第十六期:TI国际邀请赛正式打响,总奖金超过550万
2014/05/23 DOTA
[01:11:46]DOTA2-DPC中国联赛 正赛 iG vs Magma BO3 第一场 2月23日
2021/03/11 DOTA
python中的yield使用方法
2014/02/11 Python
python 用for循环实现1~n求和的实例
2019/02/01 Python
python实现两张图片拼接为一张图片并保存
2019/07/16 Python
Python使用Chrome插件实现爬虫过程图解
2020/06/09 Python
python连接手机自动搜集蚂蚁森林能量的实现代码
2021/02/24 Python
基于Html5实现的语音搜索功能
2019/05/13 HTML / CSS
Roots加拿大官网:加拿大休闲服饰品牌
2016/10/24 全球购物
美国知名的隐形眼镜电商:Contacts America
2019/11/19 全球购物
初中校园之声广播稿
2014/01/15 职场文书
社区国庆节活动方案
2014/02/05 职场文书
军训考核自我鉴定
2014/02/13 职场文书
可口可乐广告词
2014/03/20 职场文书
阅兵口号
2014/06/19 职场文书
2016教师校本培训心得体会
2016/01/08 职场文书
ubuntu安装jupyter并设置远程访问的实现
2022/03/31 Python