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实现简单拆分PDF文件的方法
Jul 30 Python
Python内置函数 next的具体使用方法
Nov 24 Python
Django中url的反向查询的方法
Mar 14 Python
神经网络(BP)算法Python实现及应用
Apr 16 Python
python读取文件名并改名字的实例
Jan 07 Python
python中tkinter的应用:修改字体的实例讲解
Jul 17 Python
PyCharm中代码字体大小调整方法
Jul 29 Python
python隐藏类中属性的3种实现方法
Dec 19 Python
python:目标检测模型预测准确度计算方式(基于IoU)
Jan 18 Python
python实现拼图小游戏
Feb 22 Python
超全Python图像处理讲解(多模块实现)
Apr 13 Python
Python爬虫进阶之爬取某视频并下载的实现
Dec 08 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
新52大事件
2020/03/03 欧美动漫
百事可乐也出咖啡了 双倍咖啡因双倍快乐
2021/03/03 咖啡文化
提示Trying to clone an uncloneable object of class Imagic的解决
2011/10/27 PHP
php冒泡排序、快速排序、快速查找、二维数组去重实例分享
2014/04/24 PHP
php中文繁体和简体相互转换的方法
2015/03/21 PHP
php获取当前url地址的方法小结
2017/01/10 PHP
Laravel使用模型实现like模糊查询的例子
2019/10/24 PHP
Javascript 事件流和事件绑定
2009/07/16 Javascript
通过JavaScript使Div居中并随网页大小改变而改变
2013/06/24 Javascript
JS保留两位小数 四舍五入函数的小例子
2013/11/20 Javascript
JavaSript中变量的作用域闭包的深入理解
2014/05/12 Javascript
JS中attr和prop属性的区别以及优先选择示例介绍
2014/06/30 Javascript
js实现点击图片改变页面背景图的方法
2015/02/28 Javascript
js实现当复选框选择匿名登录时隐藏登录框效果
2015/08/14 Javascript
jQuery实现的瀑布流加载效果示例
2016/09/13 Javascript
详解webpack分包及异步加载套路
2017/06/29 Javascript
浅谈JavaScript中的属性:如何遍历属性
2017/09/14 Javascript
vue cli使用绝对路径引用图片问题的解决
2017/12/06 Javascript
vue-cli 2.*中导入公共less文件的方法步骤
2018/11/22 Javascript
微信小程序如何通过用户授权获取手机号(getPhoneNumber)
2020/01/21 Javascript
vscode中的vue项目报错Property ‘xxx‘ does not exist on type ‘CombinedVueInstance<{ readyOnly...Vetur(2339)
2020/09/11 Javascript
[01:02:03]2014 DOTA2华西杯精英邀请赛 5 24 NewBee VS VG
2014/05/26 DOTA
python中readline判断文件读取结束的方法
2014/11/08 Python
基于Numba提高python运行效率过程解析
2020/03/02 Python
关于python 跨域处理方式详解
2020/03/28 Python
解决Keras自带数据集与预训练model下载太慢问题
2020/06/12 Python
python获取百度热榜链接的实例方法
2020/08/25 Python
CSS3实现文本垂直排列的方法
2018/07/10 HTML / CSS
KENZO官网:高田贤三在法国创立的品牌
2019/05/16 全球购物
Erwin Müller穆勒家居瑞士官网:您整个家庭的邮购公司
2019/12/28 全球购物
医生自荐信
2013/10/11 职场文书
2015年班级工作总结范文
2015/04/03 职场文书
JS一分钟在github+Jekyll的博客中添加访问量功能的实现
2021/04/03 Javascript
Python数据可视化之绘制柱状图和条形图
2021/05/25 Python
一次Mysql update sql不当引起的生产故障记录
2022/04/01 MySQL
Golang 实现 WebSockets 之创建 WebSockets
2022/04/24 Golang