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黑魔法Descriptor描述符的实例解析
Jun 02 Python
Django Channels 实现点对点实时聊天和消息推送功能
Jul 17 Python
python代码编写计算器小程序
Mar 30 Python
利用ImageAI库只需几行python代码实现目标检测
Aug 09 Python
Django项目主urls导入应用中views的红线问题解决
Aug 10 Python
python 定时器每天就执行一次的实现代码
Aug 14 Python
python装饰器原理与用法深入详解
Dec 19 Python
python各层级目录下import方法代码实例
Jan 20 Python
在pycharm中实现删除bookmark
Feb 14 Python
pycharm实现print输出保存到txt文件
Jun 01 Python
matlab xlabel位置的设置方式
May 21 Python
如何理解及使用Python闭包
Jun 01 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 生成随机验证码图片代码
2010/02/08 PHP
php缩放图片(根据宽高的等比例缩放)实例介绍
2013/06/09 PHP
php 如何获取数组第一个值
2013/08/06 PHP
php发送post请求函数分享
2014/03/06 PHP
php如何解决无法上传大于8M的文件问题
2014/03/10 PHP
PHP模拟asp.net的StringBuilder类实现方法
2015/08/08 PHP
PHP7.1新功能之Nullable Type用法分析
2016/09/26 PHP
php 截取中英文混合字符串的方法
2018/05/31 PHP
JS+CSS实现电子商务网站导航模板效果代码
2015/09/10 Javascript
jQuery操作iframe中js函数的方法小结
2016/07/06 Javascript
轻松掌握JavaScript装饰者模式
2016/08/27 Javascript
深入学习js瀑布流布局
2016/10/14 Javascript
微信小程序 页面跳转和数据传递实例详解
2017/01/19 Javascript
详解Vue-cli webpack移动端自动化构建rem问题
2018/04/07 Javascript
AngularJS使用$http配置对象方式与服务端交互方法
2018/08/13 Javascript
JS实现简单的文字无缝上下滚动功能示例
2019/06/22 Javascript
浅谈vue权限管理实现及流程
2020/04/23 Javascript
解决Vue中使用keepAlive不缓存问题
2020/08/04 Javascript
Python常用正则表达式符号浅析
2014/08/13 Python
python集合用法实例分析
2015/05/30 Python
用python与文件进行交互的方法
2018/03/01 Python
深入理解Django-Signals信号量
2019/02/19 Python
Python常用爬虫代码总结方便查询
2019/02/25 Python
python3.4 将16进制转成字符串的实例
2019/06/12 Python
Python 把序列转换为元组的函数tuple方法
2019/06/27 Python
Python facenet进行人脸识别测试过程解析
2019/08/16 Python
利用python控制Autocad:pyautocad方式
2020/06/01 Python
世界上最悠久的自行车制造商:Ribble Cycles
2017/03/18 全球购物
德国购买健身器材:AsVIVA
2017/08/09 全球购物
Cotton On南非:澳洲时尚平价品牌
2018/06/28 全球购物
匡威西班牙官网:Converse西班牙
2019/10/01 全球购物
北京SQL新华信咨询
2016/09/30 面试题
教育实习生的自我评价分享
2013/11/21 职场文书
三方协议书范本
2014/04/22 职场文书
员工工作自我评价
2014/09/26 职场文书
数学备课组工作总结
2015/08/12 职场文书