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中optionParser模块的使用方法实例教程
Aug 29 Python
Python实现建立SSH连接的方法
Jun 03 Python
KMP算法精解及其Python版的代码示例
Jun 01 Python
python matplotlib中文显示参数设置解析
Dec 15 Python
python socket网络编程之粘包问题详解
Apr 28 Python
Python实现FM算法解析
Jun 18 Python
如何运行带参数的python脚本
Nov 15 Python
python 实现保存最新的三份文件,其余的都删掉
Dec 22 Python
Django 批量插入数据的实现方法
Jan 12 Python
Django model.py表单设置默认值允许为空的操作
May 19 Python
Keras SGD 随机梯度下降优化器参数设置方式
Jun 19 Python
总结Python连接CS2000的详细步骤
Jun 23 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中用文本文件做数据库的实现方法
2008/03/27 PHP
php+mysql事务rollback&amp;commit示例
2010/02/08 PHP
解析smarty模板中类似for的功能实现
2013/06/18 PHP
PHP strip_tags保留多个HTML标签的方法
2016/05/22 PHP
php+ajax+json 详解及实例代码
2016/12/12 PHP
浅谈Laravel核心解读之Console内核
2018/12/02 PHP
javascript GUID生成器实现代码
2009/10/31 Javascript
Dom 学习总结以及实例的使用介绍
2013/04/24 Javascript
jQuery获得IE版本不准确webbrowser的解决方法
2014/02/23 Javascript
动态加载js、css等文件跨iframe实现
2014/02/24 Javascript
Javascript 数组排序详解
2014/10/22 Javascript
举例讲解如何判断JavaScript中对象的类型
2016/04/22 Javascript
基于Bootstrap使用jQuery实现简单可编辑表格
2016/05/04 Javascript
使用jQuery.form.js/springmvc框架实现文件上传功能
2016/05/12 Javascript
JavaScript函数中关于valueOf和toString的理解
2016/06/14 Javascript
带有定位当前位置的百度地图前端web api实例代码
2016/06/21 Javascript
jQuery查找节点并获取节点属性的方法
2016/09/09 Javascript
canvas雪花效果核心代码分享
2017/02/19 Javascript
原生JS实现的双色球功能示例
2018/02/02 Javascript
Servlet3.0与纯javascript通过Ajax交互的实例详解
2018/03/18 Javascript
微信小程序 swiper 组件遇到的问题及解决方法
2019/05/26 Javascript
nest.js 使用express需要提供多个静态目录的操作方法
2019/10/24 Javascript
Python 条件判断的缩写方法
2008/09/06 Python
Python列表(list)、字典(dict)、字符串(string)基本操作小结
2014/11/28 Python
利用Python抓取行政区划码的方法
2016/11/28 Python
python用win32gui遍历窗口并设置窗口位置的方法
2019/07/26 Python
python实现贪吃蛇游戏源码
2020/03/21 Python
Anaconda+VSCode配置tensorflow开发环境的教程详解
2020/03/30 Python
Python将QQ聊天记录生成词云的示例代码
2021/02/10 Python
HTML5 Canvas中使用用路径描画圆弧
2015/01/01 HTML / CSS
AmazeUI 单选框和多选框的实现示例
2020/08/18 HTML / CSS
澳大利亚领先的孕妇服装品牌:Mamaway
2018/08/14 全球购物
通信工程专业个人找工作求职信范文
2013/09/21 职场文书
年会活动策划方案
2014/01/23 职场文书
党支部活动策划方案
2014/08/18 职场文书
致创业您:正能量激励人心句子(48条)
2019/08/15 职场文书