Python 3和Tkinter-如何控制测验循环

时间:2014-12-30 16:27:00

标签: python arrays indexing tkinter

我确定答案很明显,但我无法发现它!我有一个非常基本的测验(使用Tkinter和Python 3),它使用2个数组,显示问题,然后在点击提交按钮时使用数组索引匹配输入的答案。

  1. 索引0处的问题显示两次 - 无法理解原因
  2. 分数没有正确增加 - 即使它是一个全局变量 - 它每次只显示1。
  3. 如何在到达列表结尾后让测验移至打印语句?
  4. 我已经尝试在提交函数中放置一个IF语句来检查i的值但是无法使其工作。有人可以指出我的错误吗?

    from tkinter import *
    
    global questions
    questions =["What is the name of the Simpsons' next door neighbour?","What is the name of the    school bus driver?",
            "Who runs the Kwik-e-mart?","What does Bart do at the end of the opening credits?"]
    global answers
    answers = [ "Ned Flanders","Otto","Apu","Write On The Blackboard"]
    
    global score
    score = 0
    global i
    i = 0
    
    
    
    
    def submit():
        '''runs the submit button'''
        global i
        global score
    
        question.config(text=questions[i])
        if answer.get().lower()==answers[i].lower():
            score+=1
        else:
            score=score
        i+=1
    
        scoretxt.config(text =str(score))
        answer.delete(0,END)
    
    
    window = Tk()
    window.title("Simpsons Quiz")
    window.wm_iconbitmap("homer.ico")
    window.configure(background ="#ffd600")
    
    banner = PhotoImage(file ="the-simpsons-banner.gif")
    Label(window,image = banner).grid(row = 0,columnspan = 6)
    Label(window,text = "Question : ",bg ="#ffd600",justify=LEFT).grid(row = 1,column = 0)
    Label(window,text = "Type answer here: ",bg = "#ffd600",justify=LEFT).grid(row = 3, column = 0)
    scoreLabel =Label(window,bg = "#ffd600")
    scoretxt = Label(window,text ="Your score is: ?",bg = "#ffd600")
    scoreLabel.grid(row=5,column = 2)
    scoretxt.grid(row = 6,column = 2)
    question=Label(window,bg = "white",text= questions[0],justify=LEFT)
    question.grid(row =1,column=1)
    answer = Entry(window,bg ="white",width = 30)
    answer.grid(row = 3,column=1)
    
    # make a submit button
    
    Button(window,text= "Submit",bg = "white",command = submit).grid(row = 3,column = 2)
    
    mainloop()
    

1 个答案:

答案 0 :(得分:0)

1)在递增i之前,您正在打印问题。这就是你得到两次的原因 2)由于您使用global,这始终是1。在python中,您可以使用global关键字来更改变量的范围。可悲的是,我的英语不足以解释这一点。请查看these answers 3)您可以使用try-except阻止。我在那里使用它,因为这是你得到错误的确切行。您可以扩大其范围。

from tkinter import *

questions =["What is the name of the Simpsons' next door neighbour?","What is the name of the    school bus driver?",
        "Who runs the Kwik-e-mart?","What does Bart do at the end of the opening credits?"]

answers = [ "Ned Flanders","Otto","Apu","Write On The Blackboard"]

#removed globals from here
score = 0
i = 0

def submit():
    '''runs the submit button'''
    global i
    global score

    if answer.get().lower()==answers[i].lower():
        score+=1

    i+=1 #first increment, then show the question since you already show it at startup

    try:  #since you get the IndexError on this line, I used on here
        question.config(text=questions[i])
    except IndexError:
        print ("something")

    scoretxt.config(text = "Your score is: {}".format(str(score)))
    answer.delete(0,END)


window = Tk()
window.title("Simpsons Quiz")
window.wm_iconbitmap("homer.ico")
window.configure(background ="#ffd600")

banner = PhotoImage(file ="the-simpsons-banner.gif")
Label(window,image = banner).grid(row = 0,columnspan = 6)
Label(window,text = "Question : ",bg ="#ffd600",justify=LEFT).grid(row = 1,column = 0)
Label(window,text = "Type answer here: ",bg = "#ffd600",justify=LEFT).grid(row = 3, column = 0)
scoreLabel =Label(window,bg = "#ffd600")
scoretxt = Label(window,text ="Your score is: ?",bg = "#ffd600")
scoreLabel.grid(row=5,column = 2)
scoretxt.grid(row = 6,column = 2)
question=Label(window,bg = "white",text= questions[0],justify=LEFT)
question.grid(row =1,column=1)
answer = Entry(window,bg ="white",width = 30)
answer.grid(row = 3,column=1)

# make a submit button

Button(window,text= "Submit",bg = "white",command = submit).grid(row = 3,column = 2)

mainloop()

此外,您可能希望使用字典而不是问题 - 答案列表。