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


猜你喜欢
- 什么是nodejs?Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境,nodejs允许javascri
- 模式特点:给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。我们来看一下下面这样的程序结构:c
- 这是我使用python写的第一个类(也算是学习面向对象语言以来正式写的第一个解耦的类),记录下改进的过程。分析需求最初,因为使用time模块
- Update Scanner这个Firefox附加软件也是一种很好的选择。Update Scanner可以同时跟踪多个网页,并为不同的网页设
- 前一阵子工作项目上的事情忙的焦头烂额,最近要进行部门调整将要去做新的项目。又要学习很多新的知识了,还是很兴奋激动的。今天下班回来查看了一下V
- 连接 Redisimport redisc连接方式:redis提供了2个方法1:StrictRedis:实现大部分官方的命令2:Redis:
- 在使用easyUI做前端样式展示时,遇到了文件上传的问题,而且是在弹出层中提交表单,想做到不刷新页面,所以选择了使用ajaxFileUplo
- 1、字符串的索引与获取字符串的索引方式与列表的索引方式是一样的。只不过列表是每个元素的自身就有一个索引位置,而字符串是每个字符就有一个索引位
- 贴代码:import os import sys iplist = list() ip = '192.168.1.11' #
- 一个等号 =:表示赋值 ;两个等号 ==:先转换类型再比较 ;三个等号 ===:先判断类型,如果不是同一类型直接false。
- opencv图像处理(深度学习中常用的)改变色彩空间: cv.cvtColor()cv.cvtColor(img, flag)img:原图像
- 如下所示:arrs=[2,15,48,4,5,6,7,6,4,1,2,3,6,6,7,4,6,8]f=open('test.txt&
- 在此之前,我一直都在研究JavaScript相关的反调试技巧。但是当我在网上搜索相关资料时,我发现网上并没有多少关于这方面的文章,而且就算有
- 简单的for循环打印三角形1,for循环方法实现星星三角代码:for i in range(0,5):for j in range(i+1)
- 如下所示:只对当前文件有效的操作:菜单栏->View -> Active Editor -> Use Soft Wraps
- #!/usr/bin/env python3# -*- coding: utf-8 -*-import globfrom os import
- 有一个查询如下: 代码如下:SELECT c.CustomerId, CompanyName FROM Customers c
- 最近在学习MySQL优化方面的知识。本文就数据类型和schema方面的优化进行介绍。1. 选择优化的数据类型MySQL支持的数据类型有很多,
- 加速运动,即一个物体运动时速度越来越快;减速运动,即一个物体运动时速度越来越慢。现在用Javascript来模拟这两个效果,原理就是用set
- 目录你有过摸鱼时间吗实现思路运行环境界面布局定时刷新剩余时间完整代码你有过摸鱼时间吗在互联网圈子里,常常说996上班制,但是也不乏965的,