vendredi 23 décembre 2016

Why if statement doesn't work when I compare entry input with my data?

# coding = utf8
from Tkinter import *
import ttk
import os
import sys
import random
import time

root = Tk()
questionvar = StringVar()
questionvar.set("")
answervar  = StringVar()
answervar.set("")
percent = StringVar()
percent.set("")
entrystatus = StringVar()
entrystatus.set("notready")

def importlist(event):
    global questionvar
    global answervar
    global listrand
    filename = str(listname.get()) + ".txt"
    with open(filename,"rU") as list:
        listread = list.read().splitlines()
        listrand = random.sample(listread, 10)
    list.close()

def question():
    global questionvar
    global answervar    
    global listrand
    os.system("say "+ listrand[0])
    start.config(text = "下一题")
    start.update()
    check()

def check():
    global questionvar
    global listrand
    if listrand[0] == answervar.get():
        questionvar.set("恭喜你拼写正确!")
        quest.update()
        listrand.remove(listrand[0])
    else:
        questionvar.set("正确拼写是"+str(listrand[0])+",加油哦!")
        quest.update()
        listrand.remove(listrand[0])

listname = ttk.Combobox(root, values = ["List 1","List 2","List 3","List 4","List 5","List 6","List 7","List 8","List 9","List 10","List 11","List 12","List 13","List 14","List 15","List 16","List 17","List 18","List 19","List 20","List 21","List 22","List 23","List 24","List 25","List 26","List 27","List 28","List 29","List 30","List 31","List 32","List 33","List 34","List 35","List 1-10随机","List 10-20随机","List 20-30随机","全书总测"])
listname.current(0)
listname.bind("<<ComboboxSelected>>", importlist)
listname.grid(row = 0, column = 0)

quest = Label(root, textvariable = questionvar)
quest.grid(row = 1, column = 0, columnspan = 3)

entry = Entry(root, text = "请拼写:", textvariable = answervar)
entry.bind("<Return>",check)
entry.grid(row = 2, column = 0, columnspan = 3)

start = Button(root, text = "开始测试", command = question)
start.grid(row = 3, column = 0, columnspan = 3)

percentlabel = Label(root, textvariable = percent)
percentlabel.grid(row = 4, column = 0)

root.title('听力词汇抽查')
root.mainloop()

The thing is, it runs but the check function, especially in the if statement part doesn't work, it automatically shows what happens if if listrand[0] == answervar.get(): even if I input nothing in the entry. Why?

Aucun commentaire:

Enregistrer un commentaire