python - 创建 Tkinter 数学测验

标签 python tkinter

我正在学习 python,但无法让该程序正常工作。

from Tkinter import*
import time
import tkMessageBox
import random

def Questions():    
    number1 = random.randrange(1,25,1)
    number2 = random.randrange(1,50,2)
    answer = number1 + number2
    prompt = ("Add " + str(number1) + " and " + str(number2))
    label1 = Label(root, text=prompt, width=len(prompt), bg='yellow')
    label1.pack()
    return answer

def start():
    global count_flag 
    Questions()
    count_flag = True
    count = 0.0
    while True:
        if count_flag == False:
            break
        # put the count value into the label
        label['text'] = str(count)
        # wait for 0.1 seconds
        time.sleep(0.1)
        # needed with time.sleep()
        root.update()
        # increase count
        count += 0.1

def Submit(answer, entryWidget):
     """ Display the Entry text value. """
     global count_flag

     count_flag = False
     print answer

     if entryWidget.get().strip() == "":
         tkMessageBox.showerror("Tkinter Entry Widget", "Please enter a number.")

     if int(answer) != entryWidget.get().strip():
         tkMessageBox.showinfo("Answer", "INCORRECT!")
     else:
         tkMessageBox.showinfo("Answer", "CORRECT!")

# create a Tkinter window
root = Tk()

root.title("Math Quiz")
root["padx"] = 40
root["pady"] = 20   

# Create a text frame to hold the text Label and the Entry widget
textFrame = Frame(root)

#Create a Label in textFrame
entryLabel = Label(textFrame)
entryLabel["text"] = "Answer:"
entryLabel.pack(side=LEFT)

# Create an Entry Widget in textFrame
entryWidget = Entry(textFrame)
entryWidget["width"] = 50
entryWidget.pack(side=LEFT)

textFrame.pack()

#directions     
directions = ('Click start to begin. You will be asked a series of questions like the one below.')
instructions = Label(root, text=directions, width=len(directions), bg='orange')
instructions.pack()

# this will be a global flag
count_flag = True

answer = Questions()

Sub = lambda: Submit(answer, entryWidget)

# create needed widgets
label = Label(root, text='0.0')
btn_submit = Button(root, text="Submit", command = Sub)
btn_start = Button(root, text="Start", command = start)
btn_submit.pack()
btn_start.pack()
label.pack()


# start the event loop
root.mainloop()

它只是说“不正确!”每次我按下“提交”时,无论我在文本框中输入什么内容。任何建议,将不胜感激。谢谢,斯科特

最佳答案

左边是整数,右边是字符串,所以总是 False:

int(answer) != entryWidget.get().strip()

你可以尝试:

int(answer) != int(entryWidget.get().strip())

关于python - 创建 Tkinter 数学测验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9346275/

相关文章:

python - 将 Spark DataFrame 写入 Hive 表时的内存分配问题

python - 如何返回与重复模式匹配的整个非拉丁字符串,例如 AAB 或 ABB

python - Tkinter 在那之后幸存下来时钟倒带

Python/Django 建模问题

python - 使用 PySoX 在文件转换上设置 "sample rate"属性?

python - 如何禁止选择 Tkinter Listbox 小部件的自由部分作为项目?

python - 类型错误 : must be str or None, 不是框架

Python tkinter : access child widgets of a widget

python - Linux 上的 psutil 显示没有 CPU 核心温度

python - 如何使用 Tkinter 上的入口小部件通过输入参数来执行函数?