如何在Tkinter计算器中存储数字以实现运算功能?
问题分析与解决
你的核心问题在于操作数和运算符没有被持久化存储——opt_press里的number和num是局部变量,每次调用函数都会重新赋值,无法保留第一次输入的数值。当按下=时,只能读取当前显示的内容,自然无法完成跨步骤的计算。
修改方案
我们需要用全局变量来保存计算状态,包括第一个操作数、选中的运算符,以及是否已经触发了运算符输入的标记。以下是调整后的完整代码:
from tkinter import * import utils # 全局变量保存计算状态 first_num = None operator = None is_operator_pressed = False window = Tk() window.geometry("400x500") # 补充窗口初始化,原代码缺失 display = Entry(window, borderwidth=10, bg='#EDF1D6', font=('Arial', 16)) display.place(x=0, y=0, width=utils.width_prct(75), height=utils.height_prct(15)) def btn_press(btn): global is_operator_pressed current = display.get() display.delete(0, END) # 如果刚按过运算符,清空显示后输入新数字;否则追加数字 if is_operator_pressed: display.insert(0, str(btn)) is_operator_pressed = False else: display.insert(0, str(current) + str(btn)) operators = ['C', 'x', '/', '+', '-', '=', '^'] def opt_press(btn1): global first_num, operator, is_operator_pressed # 处理清除键 if btn1 == operators[0]: display.delete(0, END) first_num = None operator = None is_operator_pressed = False return # 处理等号以外的运算符 if btn1 != operators[5]: # 保存第一个操作数和当前运算符 try: first_num = int(display.get()) except ValueError: display.delete(0, END) display.insert(0, "错误") return operator = btn1 is_operator_pressed = True display.delete(0, END) # 平方运算直接计算 if btn1 == operators[1]: result = first_num ** 2 display.insert(0, result) first_num = result is_operator_pressed = False # 处理等号 else: if first_num is None or operator is None: display.insert(0, "错误") return try: second_num = int(display.get()) except ValueError: display.delete(0, END) display.insert(0, "错误") return # 根据运算符计算结果 result = 0 if operator == '+': result = first_num + second_num elif operator == '-': result = first_num - second_num elif operator == '/': if second_num == 0: display.insert(0, "除零错误") return result = first_num / second_num elif operator == '^': result = first_num ** second_num # 显示结果并重置状态 display.delete(0, END) display.insert(0, result) first_num = result is_operator_pressed = False # 数字按钮布局(补充完整0-9的位置,原代码只放了1、4、7) btn_positions = { 7: (0, utils.height_prct(15)), 8: (utils.width_prct(25), utils.height_prct(15)), 9: (utils.width_prct(50), utils.height_prct(15)), 4: (0, utils.height_prct(35)), 5: (utils.width_prct(25), utils.height_prct(35)), 6: (utils.width_prct(50), utils.height_prct(35)), 1: (0, utils.height_prct(55)), 2: (utils.width_prct(25), utils.height_prct(55)), 3: (utils.width_prct(50), utils.height_prct(55)), 0: (0, utils.height_prct(75)) } for num, pos in btn_positions.items(): btn = Button(window, text=num, bg='#9DC08B', fg='black', font=('Arial', 14), command=lambda num=num: btn_press(num)) btn.place(x=pos[0], y=pos[1], width=utils.width_prct(25), height=utils.height_prct(20)) # 运算符按钮布局 operator_positions = { 'C': (utils.width_prct(75), 0), 'x': (utils.width_prct(75), utils.height_prct(15)), '/': (utils.width_prct(75), utils.height_prct(35)), '+': (utils.width_prct(75), utils.height_prct(55)), '-': (utils.width_prct(75), utils.height_prct(75)), '=': (utils.width_prct(50), utils.height_prct(75)), '^': (utils.width_prct(25), utils.height_prct(75)) } for op, pos in operator_positions.items(): btn_o = Button(window, text=op, bg='#609966', fg='black', font=('Arial', 14), command=lambda op=op: opt_press(op)) btn_o.place(x=pos[0], y=pos[1], width=utils.width_prct(25), height=utils.height_prct(20)) window.mainloop()
关键修改点
- 新增全局变量
first_num、operator、is_operator_pressed,用于跨函数保存计算状态。 btn_press函数判断is_operator_pressed状态,决定是追加数字还是清空后输入新数字。opt_press函数:- 按运算符时保存第一个操作数和运算符,标记
is_operator_pressed为True。 - 按
=时读取第二个操作数,根据运算符完成计算,显示结果并重置状态。 - 增加异常处理,避免非数字输入导致崩溃,处理除零错误。
- 补充了所有数字和运算符的布局,原代码只放置了部分按钮。
- 按运算符时保存第一个操作数和运算符,标记
内容的提问来源于stack exchange,提问作者Nanika
相关产品推荐
相关产品推荐

