python tkinter实现简单计算器功能
作者:MrNoboday 发布时间:2022-06-16 22:34:09
标签: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()
来源:https://blog.csdn.net/MrNoboday/article/details/90720553
0
投稿
猜你喜欢
- 序言:php错误就是会使脚本运行不正常的情况。php的错误有很多种,包括warning、notice、deprecated、fetal er
- 我们平时生活的娱乐中,看电影是大部分小伙伴都喜欢的事情。周围的人总会有意无意的在谈论,有什么影片上映,好不好看之类的话题,没事的时候谈论电影
- 我就废话不多说了,直接上代码吧!import Imagefrom datetime import datetimeimport osstr
- 春节休息了几天,今天上班第一天,最近混twitter混得比较多,经常要压缩URL,以前做了个书签用http://is.gd/压缩,后来发现了
- python使用folium 库生成地图网页的具体代码,供大家参考,具体内容如下folium 官网import foliumimport p
- 前言在使用PC时与PC交互的主要途径是看屏幕显示、听声音,点击鼠标和敲键盘等等。在自动化办公的趋势下,繁琐的工作可以让程序自动完成。比如自动
- 引用Nmap库实现扫描功能,本节课比较简单一看就会。编写环境:Python2.x编写:首先安装Nmap程序,并添加环境变量pip insta
- 一.问题描述在二维数组的遍历中,我们经常使用双层for循环。在某些时候,我们并不需要遍历整个二维数组。当条件满足时就应该终止for循环。但是
- 这个是升级的版本,比较进阶一点的,相对与之前的文件管理系统,数据只是存储到了内存中,而不是存储到硬盘上,我们想让文件存储到硬盘上的话,一个是
- 数据库并行访问,也就是两个或两以上用户同时访问同一数据,这也是数据库引擎如何设计和实现适度反应所面临的最大问题。设计优良、性能卓越的数据库引
- Jabob Nielsen提出的10个可用性原则中有一个原则-防止出错(Error Prevention)。官网中有对这一原则的具体解释,这
- 以下是YUI中不建议用的一些说明: 表达式的问题就在于它的计算频率要比
- 本文实例讲述了Python使用crontab模块设置和清除定时任务操作。分享给大家供大家参考,具体如下:centos7下安装Python的p
- 循环写入字典key、value、删除指定的键值对:原文本‘jp_url.txt'每行元素以逗号分隔:host_key,product
- 1、前言 MySQL 是完全网络化的跨平台关系型数据库系统,同时是具有客户机/服务器体系结构的分布式数据库管理系统。它具有功能强、使用简便、
- Python操作注册表步骤之1.打开注册表对注册表进行操作前,必须打开注册表。在Python中,可以使用以下两个函数:RegOpenKey和
- 效果图基本思路在 OpenCV 中使用VideoCapture方法初始化视频渲染对象创建灰度图像导入预训练模型,识别脸部和人脸标志计算上唇和
- 代码如下import unittestdir = "D:\\work_doc\\pycharm2\\python_Basics&q
- post接收字符串def subscription(request): msg = request.POST.get('
- 设计与开发之间本有一线界限,但当时代步入又一个十年,这个线变得更加模糊甚至感觉不到它的存在。使用PS设计网页版面,足矣?或许五年前是吧!现在