You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter开发数学闪卡程序答题提示数字不更新如何修复

问题现象

开发Tkinter数学闪卡程序时,随机出题功能出现异常:界面上展示的题目数字可以正常刷新,但答题后的结果提示始终固定显示程序启动时第一次生成的初始数字,无法同步匹配当前作答的题目数值。

原问题代码如下:

from tkinter import messagebox
from random import randint

def clicked():
    messagebox.showinfo('Rules of the Game', '1. You are going to be provided with basic arithmetic (Adding, Subtracting, Multiplying)'
    '\n2. You are going to answer them'
    '\n3. Based on the answer you gave, it will keep track of the correct and incorrect answers including how many in total you have answered')                       

root = Tk()
root.title('Math Flash Cards')
root.geometry('900x500')

Title = Label(root, text="Title", font=('Arial', 30))
Title.grid(row = 0, column = 4)

Rule_btn = Button(root, text="Click Here for Rules", font=('Arial'), command=clicked)
Rule_btn.grid(row = 3, column = 4)

#Create random flashcard numbers
def math_random():
    global nums
    number_1 = randint(1,10)
    number_2 = randint(1,10)

    add_1.configure(text = number_1)
    add_2.configure(text = number_2)
    

def answer_add():
    answers = number_1 + number_2
    if int(add_ans.get()) == answers:
        response = 'Correct ' + str(number_1) + ' + ' + str(number_2) + ' = ' + str(answers)
    elif int(add_ans.get()) != answers:
        response =  'Wrong! ' + str(number_1) + ' - ' + str(number_2) + ' = ' + str(answers)
    answer_message.config(text = response)
    add_ans.delete(0, 'end')
    math_random()
    

global nums
number_1 = randint(1,10)
number_2 = randint(1,10)


global add_1
global add_2
add_1 = Label(root, text='+', font=('Arial', 20))
add_2 = Label(root, text='+', font=('Arial', 20))
math_operator = Label(root, text='+', font=('Arial', 20))


add_1.grid(row = 5, column = 3)
math_operator.grid(row = 5, column = 4)
add_2.grid(row = 5, column = 5)


add_1.configure(text = number_1)
add_2.configure(text = number_2)


global add_ans
add_ans = Entry(root, font = ('Arial', 14))
add_ans.grid(row = 7, column = 4)
add_ans_btn = Button(root, text ='Click', font = ('Arial'), command= answer_add)
add_ans_btn.grid(row = 7, column = 5)

global answer_message
answer_message = Label(root, text='Type you answer with the provided space above', font=('Arial'))
answer_message.grid(row = 10, column = 4)
故障原因
  • 核心问题是变量作用域错误:math_random()函数内生成新的number_1、number_2时,没有声明这两个变量为全局变量,函数内赋值的数值是仅在函数内部生效的局部变量,不会修改全局作用域下的初始number_1、number_2值。后续answer_add()函数计算答案、拼接提示文本时,读取的始终是程序启动时全局生成的第一组数字,因此提示内容不会跟着新题目更新。
  • 附带问题1:代码缺失Tkinter组件的导入语句,也没有写窗口主循环,直接运行会报错、窗口无法常驻。
  • 附带问题2:加法题回答错误时,提示文本里错误写了减号运算符,和实际题目不符。
  • 附带问题3:代码里声明了从未使用的nums全局变量,属于冗余代码。
修复方案
  1. 在math_random()函数开头添加global number_1, number_2声明,让函数内生成的新随机数直接修改全局变量值,保证答题校验时读取的是当前题目的数字。
  2. 补全Tkinter相关组件的导入语句,添加窗口主循环。
  3. 修正错误提示里的运算符笔误,删除无用的冗余变量声明。

修复后的可运行完整代码:

from tkinter import Tk, Label, Button, Entry, messagebox
from random import randint

def clicked():
    messagebox.showinfo('Rules of the Game', '1. You are going to be provided with basic arithmetic (Adding, Subtracting, Multiplying)'
    '\n2. You are going to answer them'
    '\n3. Based on the answer you gave, it will keep track of the correct and incorrect answers including how many in total you have answered')                       

root = Tk()
root.title('Math Flash Cards')
root.geometry('900x500')

Title = Label(root, text="Math Flash Cards", font=('Arial', 30))
Title.grid(row = 0, column = 4)

Rule_btn = Button(root, text="Click Here for Rules", font=('Arial'), command=clicked)
Rule_btn.grid(row = 3, column = 4)

# 生成随机题目数字
def math_random():
    global number_1, number_2
    number_1 = randint(1,10)
    number_2 = randint(1,10)
    add_1.configure(text = number_1)
    add_2.configure(text = number_2)
    
# 校验加法答案
def answer_add():
    answers = number_1 + number_2
    if int(add_ans.get()) == answers:
        response = 'Correct ' + str(number_1) + ' + ' + str(number_2) + ' = ' + str(answers)
    else:
        response =  'Wrong! ' + str(number_1) + ' + ' + str(number_2) + ' = ' + str(answers)
    answer_message.config(text = response)
    add_ans.delete(0, 'end')
    math_random()
    
# 初始化第一题
number_1 = randint(1,10)
number_2 = randint(1,10)

add_1 = Label(root, text=number_1, font=('Arial', 20))
add_2 = Label(root, text=number_2, font=('Arial', 20))
math_operator = Label(root, text='+', font=('Arial', 20))

add_1.grid(row = 5, column = 3)
math_operator.grid(row = 5, column = 4)
add_2.grid(row = 5, column = 5)

add_ans = Entry(root, font = ('Arial', 14))
add_ans.grid(row = 7, column = 4)
add_ans_btn = Button(root, text ='Click', font = ('Arial'), command= answer_add)
add_ans_btn.grid(row = 7, column = 5)

answer_message = Label(root, text='Type you answer with the provided space above', font=('Arial'))
answer_message.grid(row = 10, column = 4)

root.mainloop()

内容的提问来源于stack exchange,提问作者Legit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.01 13:15:40