python tkinter实现简单计算器功能
作者:MrNoboday 时间:2022-06-16 22:34:09
本文实例为大家分享了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
标签:python,tkinter,计算器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
asp下查询xml的实现代码
2011-04-19 10:37:00
Web表单设计:表单结构
2011-04-22 12:32:00
flask设置cookie
2022-03-19 21:13:01
GoJs的文本绘图模板TextBlock使用实例
2023-08-26 16:01:00
![](https://img.aspxhome.com/file/2023/6/56246_0s.jpg)
python3反转字符串的3种方法(小结)
2022-05-03 22:59:01
pandas使用fillna函数填充NaN值的代码实例
2023-09-29 05:51:48
ThinkPHP5中如何使用redis
2023-06-13 01:09:10
![](https://img.aspxhome.com/file/2023/9/55499_0s.png)
layui table 获取分页 limit的方法
2023-08-24 13:44:56
![](https://img.aspxhome.com/file/2023/2/56012_0s.jpg)
python numpy存取文件的方式
2021-04-15 18:31:56
![](https://img.aspxhome.com/file/2023/2/96932_0s.png)
星球大战与Python之间的那些事
2023-05-25 13:49:35
![](https://img.aspxhome.com/file/2023/5/105295_0s.jpg)
再论Javascript的类继承
2010-06-26 12:48:00
selenium自动化测试简单准备
2023-02-07 13:04:12
![](https://img.aspxhome.com/file/2023/4/75904_0s.png)
详解pandas绘制矩阵散点图(scatter_matrix)的方法
2022-06-09 18:52:34
![](https://img.aspxhome.com/file/2023/7/107207_0s.png)
添加到各大流行网摘 书签的代码
2008-04-20 14:15:00
提高MYSQL查询效率的三个有效的尝试
2009-02-27 16:08:00
Python分割单词和转换命名法的实现
2023-11-24 00:06:16
Redux saga异步管理与生成器详解
2023-07-24 02:53:52
![](https://img.aspxhome.com/file/2023/9/55999_0s.png)
使用pytorch搭建AlexNet操作(微调预训练模型及手动搭建)
2023-05-04 05:09:51
![](https://img.aspxhome.com/file/2023/9/78509_0s.jpg)
Python安装并操作redis实现流程详解
2023-06-15 00:17:22
![](https://img.aspxhome.com/file/2023/7/100287_0s.png)
python numpy 按行归一化的实例
2023-04-29 09:14:53