Python Tkinter实现简易计算器功能
作者:Cullenyy 时间:2023-08-08 09:53:30
闲暇时间用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
标签:Python,Tkinter,计算器
0
投稿
猜你喜欢
python如何读取和存储dict()与.json格式文件
2021-12-07 16:13:36
pycharm快捷键汇总
2022-12-30 22:43:40
Python实现基于HTTP文件传输实例
2021-10-23 13:24:53
Python2.x与3.x版本有哪些区别
2023-11-01 08:34:31
python代码实现烟花实例
2022-09-13 18:09:18
什么是 XML Web Service
2008-09-05 17:21:00
python实现随机梯度下降(SGD)
2021-04-15 19:41:20
python中文分词教程之前向最大正向匹配算法详解
2023-07-23 12:51:22
python实现提取str字符串/json中多级目录下的某个值
2022-02-25 23:07:15
python绘制分组对比柱状图
2021-01-03 06:48:26
keras使用Sequence类调用大规模数据集进行训练的实现
2021-01-03 20:24:35
Python使用itchat 功能分析微信好友性别和位置
2023-09-24 15:57:12
Python 平方列表中每个数字的多种操作
2023-11-14 03:53:00
Python中提取人脸特征的三种方法详解
2023-11-06 09:38:04
将Python文件打包成.EXE可执行文件的方法
2023-05-26 20:25:50
python3.7中安装paddleocr及paddlepaddle包的多种方法
2022-08-11 00:00:57
oracle用什么SQL语句判断表存不存在
2010-07-23 13:23:00
数据库中identity字段不必是系统产生的唯一值 性能优化方法(新招)
2011-09-30 11:26:06
python网络编程之读取网站根目录实例
2021-03-07 03:04:35
python list等分并从等分的子集中随机选取一个数
2022-07-06 05:24:04