Python Tkinter实现简易计算器功能

 更新时间:2018年01月30日 09:11:04   作者:Cullenyy  
这篇文章主要为大家详细介绍了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() 

下面是运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Python tkinter之ComboBox(下拉框)的使用简介

    Python tkinter之ComboBox(下拉框)的使用简介

    这篇文章主要介绍了Python tkinter之ComboBox(下拉框)的使用简介,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2021-02-02
  • python基础学习之生成器与文件系统知识总结

    python基础学习之生成器与文件系统知识总结

    本文是参考《python数据分析》的附录对生成器和文件系统结合案例的一个简单回顾,文中对python生成器与文件系统作了非常详细的介绍,对正在学习python的小伙伴们有很好地帮助,需要的朋友可以参考下
    2021-05-05
  • 使用Python获取公众号下所有的文章

    使用Python获取公众号下所有的文章

    我比较喜欢看公众号,有时遇到一个感兴趣的公众号时,都会感觉相逢恨晚,想一口气看完所有历史文章。本文主要介绍了使用Python获取公众号下所有的文章,感兴趣的可以了解一下
    2021-06-06
  • Python3使用SMTP发送带附件邮件

    Python3使用SMTP发送带附件邮件

    这篇文章主要为大家详细介绍了Python3使用SMTP发送带附件邮件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • Python之pandas读写文件乱码的解决方法

    Python之pandas读写文件乱码的解决方法

    下面小编就为大家分享一篇Python之pandas读写文件乱码的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • 如何解决django-celery启动后迅速关闭

    如何解决django-celery启动后迅速关闭

    在本篇文章里小编给大家整理的是关于django-celery启动后迅速关闭的解决方法,有需要的朋友们学习下。
    2019-10-10
  • Python中数字(Number)数据类型常用操作

    Python中数字(Number)数据类型常用操作

    本文主要介绍了Python中数字(Number)数据类型常用操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • Tensorflow 2.1完成对MPG回归预测详解

    Tensorflow 2.1完成对MPG回归预测详解

    这篇文章主要为大家介绍了Tensorflow 2.1完成对MPG回归预测详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • python中import与from方法总结(推荐)

    python中import与from方法总结(推荐)

    这篇文章主要介绍了python中import与from方法总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • PyTorch 中的傅里叶卷积实现示例

    PyTorch 中的傅里叶卷积实现示例

    这篇文章主要介绍了PyTorch 中的傅里叶卷积实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12

最新评论