Python Tkinter实现简易计算器功能
作者:Cullenyy 发布时间:2023-08-08 09:53:30
标签:Python,Tkinter,计算器
闲暇时间用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()
下面是运行结果:
来源:http://blog.csdn.net/wangyiyan315/article/details/19435081
0
投稿
猜你喜欢
- 本文实例讲述了wxPython中listbox用法。分享给大家供大家参考。具体如下:# load a listbox with names,
- 刚来这个公司,熟悉了环境,老大就开始让我做一个迁移、修改代码的工作,我想说的是,这种工作真没劲~~,看别人的代码、改别人的代码、这里改个变量
- 装饰器一、介绍器:代表函数的意思。装饰器本质就是是函数功能:装饰其他函数,就是为其他函数添加附加功能 被装饰函数感受不到装饰器的存
- Atom是一款功能强大的跨平台编辑器,插件化的解决方案为atom社区的繁荣奠定了基础。任何人都可以把自己做的组件贡献在github上,并能方
- 撰写时间:2017.5.23一维数组1.numpy初始化一维数组a = np.array([1,2,3]);print a.shape输出的
- NumPy数组的维数称为秩(rank),一维数组的秩为1,二维数组的秩为2,以此类推。在NumPy中,每一个线性的数组称为是一个轴(axes
- 前言引用一张比较清晰易懂的图php伪协议是php自己支持的一种协议与封装协议,简单来说就是php定义的一种特殊访问资源的方法。常见的php伪
- //匹配中文 数字 字母 下划线 var checkIn
- 本文主要是对flask中的before_request与after_request用法做一个简单的分析,具体实例和介绍如下。使用before
- 一、函数list(1)定义:用打开的文件作为参数,把文件内的每一行内容作为一个元素(2)格式:list(文件)(3)例子:with open
- 本文实例讲述了python中list循环语句用法。分享给大家供大家参考。具体用法分析如下:Python 的强大特性之一就是其对 list 的
- 前端时间写了一篇《利用CSS框架进行高效率的站点开发》,有不少朋友问我相关的问题。很早5key就在公司进行CSS框架的架构,也对不少朋友提出
- 前言:图像处理是常用的技术,python 拥有丰富的第三方扩展库,Pillow 是 Python3 最常用的图像处理库,目前最高版本5.2.
- 其中x1,y1;x2,y2分别表示两个矩形框的中心点def calcIOU(x1, y1, w1, h1, x2, y2, w2, h2):
- 这是来自于Steven D编写的WEB前端开发设计要点的内容。虽然许多设计师已非常熟练的使用了Web标准,让人遗憾的是有很多细节的排版处理仍
- 利用python+ffmpeg合并B站视频及格式转换 B站客户端下载的视频一般有两种格式:早期的多为blv格式(由flv格式转换而来,音视频
- 动态展示这是一个动态图哦导读兄弟们可以收藏一下哦!情人节可以送出去,肥学找了几朵python写的花给封装好送给大家。不是多炫酷但是有足够的用
- 本文实例讲述了python中黄金分割法实现方法。分享给大家供大家参考。具体实现方法如下:''' a,b = brac
- 樂思蜀将SEO工作中所需要的301转向代码进行了整理,收藏并分享,以备查阅。1、IIS下301设置 Internet信息服务管理器 ->
- 在ASP中,你可通过VBScript和其他方式调用自程序。实例:调用使用VBScript的子程序如何从ASP调用以VBScript编写的子程