python tkinter实现简单计算器功能

 更新时间:2022年01月29日 11:16:03   作者:MrNoboday  
这篇文章主要为大家详细介绍了python tkinter实现简单计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了python tkinter实现简单计算器的具体代码,供大家参考,具体内容如下

效果图

直接上代码

import tkinter as tk

input_num_ls = []
first_num = None
calculator_method = None


def get_num(ls):

    new_ls = [10 ** i * float(num) for i, num in enumerate(ls)]

    ls_sum = sum(new_ls)

    if int(ls_sum) == ls_sum:
        return int(ls_sum)
    
    else:
        return ls_sum


def append_num(num):
    global input_num_ls
    if len(num) < 10:
        input_num_ls.append(num)
    else:
        input_num_ls.append(num[:10])

    current_value.set(get_num(input_num_ls))

    print(input_num_ls)


def append_calculator(method):
    global input_num_ls, first_num, calculator_method
    calculator_method = method
    first_num = get_num(input_num_ls)
    input_num_ls = []
    print('method', calculator_method)


def calculator_result():
    global first_num, input_num_ls, calculator_method

    second_num = get_num(input_num_ls)

    input_num_ls.clear()

    if calculator_method == '+':
        current_value.set(second_num + first_num)
        input_num_ls.append(str(second_num + first_num))

    elif calculator_method == '-':
        current_value.set(first_num - second_num)
        input_num_ls.append(str(first_num - second_num))

    elif calculator_method == '*':
        current_value.set(first_num * second_num)
        input_num_ls.append(str(second_num * first_num))

    elif calculator_method == '/':
        current_value.set(first_num / second_num)
        input_num_ls.append(str(first_num / second_num))

    print(first_num, second_num, calculator_method)


def clear():
    global first_num, input_num_ls, calculator_method
    first_num = None
    input_num_ls = []
    calculator_method = None
    current_value.set(0)


def func():
    pass


# 主体窗口
window = tk.Tk()

# 设置窗口 标题
window.title('简易计算器')

# 设置窗口 宽高
window.geometry('400x300')

# 添加user显示屏幕背景
screen_area = tk.Frame(width='400', height='100', bg='#ddd')
# 放置到window中
screen_area.pack()

# 示例设置显示的数据类
current_value = tk.StringVar()
current_value.set(0)

# 数字显示框
# anchor  文本相对于标签中心的位置   默认是center N S W E
show_screen_label = tk.Label(screen_area, textvariable=current_value, bg='white', width='400', height='2', font={'黑体', 40, 'bold'}, anchor='e')
show_screen_label.pack(padx=10, pady=6)

# 按键区域
button_area = tk.Frame(width='300', height='300', bg='#ccc')
button_area.pack(padx=10, pady=5)

# 添加button
tk.Button(button_area, text='C', width='5', height='1', command=lambda: clear()).grid(row='1', column='0')
tk.Button(button_area, text='+', width='5', height='1', command=lambda: append_calculator('+')).grid(row='1', column='1')
tk.Button(button_area, text='-', width='5', height='1', command=lambda: append_calculator('-')).grid(row='1', column='2')
tk.Button(button_area, text='*', width='5', height='1', command=lambda: append_calculator('*')).grid(row='1', column='3')
tk.Button(button_area, text='7', width='5', height='1', command=lambda: append_num('7')).grid(row='2', column='0')
tk.Button(button_area, text='8', width='5', height='1', command=lambda: append_num('8')).grid(row='2', column='1')
tk.Button(button_area, text='9', width='5', height='1', command=lambda: append_num('9')).grid(row='2', column='2')
tk.Button(button_area, text='/', width='5', height='1', command=lambda: append_calculator('/')).grid(row='2', column='3')
tk.Button(button_area, text='4', width='5', height='1', command=lambda: append_num('4')).grid(row='3', column='0')
tk.Button(button_area, text='5', width='5', height='1', command=lambda: append_num('5')).grid(row='3', column='1')
tk.Button(button_area, text='6', width='5', height='1', command=lambda: append_num('6')).grid(row='3', column='2')
tk.Button(button_area, text='=', width='5', height='1', command=lambda: calculator_result()).grid(row='3', column='3')
tk.Button(button_area, text='1', width='5', height='1', command=lambda: append_num('1')).grid(row='4', column='0')
tk.Button(button_area, text='2', width='5', height='1', command=lambda: append_num('2')).grid(row='4', column='1')
tk.Button(button_area, text='3', width='5', height='1', command=lambda: append_num('3')).grid(row='4', column='2')
tk.Button(button_area, text='C', width='5', height='1', command=lambda: clear()).grid(row='4', column='3')

window.mainloop()

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

相关文章

  • opencv与numpy的图像基本操作

    opencv与numpy的图像基本操作

    这篇文章主要介绍了opencv与numpy的图像基本操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • Tensorflow Summary用法学习笔记

    Tensorflow Summary用法学习笔记

    这篇文章主要介绍了Tensorflow Summary用法学习笔记,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • 用Python解决计数原理问题的方法

    用Python解决计数原理问题的方法

    计数原理是数学中的重要研究对象之一,分类加法计数原理、分步乘法计数原理是解决计数问题的最基本、最重要的方法,也称为基本计数原理,它们为解决很多实际问题提供了思想和工具。本文教大家怎么用Python解决在数学中遇到的计数原理问题。
    2016-08-08
  • 玩转python爬虫之cookie使用方法

    玩转python爬虫之cookie使用方法

    Cookie用于服务器实现会话,用户登录及相关功能时进行状态管理,这篇文章主要介绍了使用python处理cookie的方法,感兴趣的小伙伴们可以参考一下
    2016-02-02
  • TensorFlow实现MLP多层感知机模型

    TensorFlow实现MLP多层感知机模型

    这篇文章主要为大家详细介绍了TensorFlow实现MLP多层感知机模型,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • Python异步编程之协程任务的调度操作实例分析

    Python异步编程之协程任务的调度操作实例分析

    这篇文章主要介绍了Python异步编程之协程任务的调度操作,结合实例形式分析了Python异步编程中协程任务的调度相关原理、实现方法与操作注意事项,需要的朋友可以参考下
    2020-02-02
  • centos7中安装python3.6.4的教程

    centos7中安装python3.6.4的教程

    Python3.6.4官方版是一款在适合开发人员使用的windows系统上运行的脚本语言工具,Python3.6.4官方版是目前程序设计从业者必学的语言之一。这篇文章给大家介绍了centos7中安装python3.6.4的教程,感兴趣的朋友一起看看吧
    2019-12-12
  • 调整Jupyter notebook的启动目录操作

    调整Jupyter notebook的启动目录操作

    这篇文章主要介绍了调整Jupyter notebook的启动目录操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • Python使用Pycrypto库进行RSA加密的方法详解

    Python使用Pycrypto库进行RSA加密的方法详解

    RSA加密算法是一种强大的公钥加密算法,安全性很高,这里我们来看一下Python使用Pycrypto库进行RSA加密的方法详解,需要的朋友可以参考下
    2016-06-06
  • Windows系统下pycharm中的pip换源

    Windows系统下pycharm中的pip换源

    这篇文章主要介绍了Windows系统下pycharm中的pip换源,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02

最新评论