Python:由于while循环,Tkinter停止

时间:2015-08-28 15:57:48

标签: python loops while-loop tkinter

我正在尝试在程序中运行while循环,但是当while循环到位时,代码停止,并且tkinter窗口不会打开。我该如何解决这个问题?它应该是代码写出两个随机数,然后当输入正确的答案时,它应该重新循环。

from tkinter import *
import random

root = Tk() 

#Frames
topFrame = Frame(root) # I want an invisible container in root
topFrame.pack()
bottomFrame = Frame(root) # I want an invisible container in root
bottomFrame.pack(side=BOTTOM)
#End Of Frames

#Addition Question Maker



AnswerBox = Entry(topFrame)
AnswerBox.grid(row=0,column=4)
EqualsSign = Label(topFrame, text="=").grid(row=0,column=3)
AdditionSign = Label(topFrame, text="+").grid(row=0,column=1)

NewQuestion = True

while NewQuestion == True:
    AdditionQuestionLeftSide = random.randint(0, 10)
    AdditionQuestionRightSide = random.randint(0, 10)
    global Total
    Total = AdditionQuestionLeftSide + AdditionQuestionRightSide
    AdditionQuestionRightSide = Label(topFrame, text= AdditionQuestionRightSide).grid(row=0,column=0)
    AdditionQuestionLeftSide= Label(topFrame, text= AdditionQuestionLeftSide).grid(row=0,column=2)
    answer = None


def OutputAnswerText(event):
    global answer
    answer = AnswerBox.get()
    if Total == int(answer):
      Correct = Label(topFrame, text="Correct").grid(row=2,column=3)
      NewQuestion = True
    else:
       Correct = Label(topFrame, text="Wrong").grid(row=2,column=3)

AnswerBox.bind('<Return>', OutputAnswerText)

root.mainloop() 

2 个答案:

答案 0 :(得分:0)

我建议将NewQuestion作为一个函数,而不是使用while循环。该函数最初被调用,然后如果答案是正确的,则再次调用该函数。这是我的函数代码,以及一个自动输入删除选项,以便在输入正确答案后无需退回答案。

from tkinter import *
import random

root = Tk() 

#Frames
topFrame = Frame(root) # I want an invisible container in root
topFrame.pack()
bottomFrame = Frame(root) # I want an invisible container in root
bottomFrame.pack(side=BOTTOM)
#End Of Frames

#Addition Question Maker



AnswerBox = Entry(topFrame)
AnswerBox.grid(row=0,column=4)
EqualsSign = Label(topFrame, text="=").grid(row=0,column=3)
AdditionSign = Label(topFrame, text="+").grid(row=0,column=1)


def NewQuestion():
    AdditionQuestionLeftSide = random.randint(0, 10)
    AdditionQuestionRightSide = random.randint(0, 10)
    global Total
    Total = AdditionQuestionLeftSide + AdditionQuestionRightSide
    AdditionQuestionRightSide = Label(topFrame, text= AdditionQuestionRightSide).grid(row=0,column=0)
    AdditionQuestionLeftSide= Label(topFrame, text= AdditionQuestionLeftSide).grid(row=0,column=2)
    answer = None
    return

NewQuestion()

def OutputAnswerText(event):
    global answer
    global AnswerBox
    answer = AnswerBox.get()
    if Total == int(answer):
      Correct = Label(topFrame, text="Correct").grid(row=2,column=3)
      AnswerBox.delete(0, END)
      NewQuestion()
    else:
       Correct = Label(topFrame, text="Wrong").grid(row=2,column=3)

AnswerBox.bind('<Return>', OutputAnswerText)

root.mainloop() 

答案 1 :(得分:-1)

你有一个无限循环:

while NewQuestion == True:

NewQuestion没有地方可以变为假(并且循环中没有中断)。所以循环是无限的。

此外:

EqualsSign = Label(topFrame, text="=").grid(row=0,column=3)

无效,因为grid会返回None。如果要保留对窗口小部件的引用,则必须使用两行版本,如:

AnswerBox = Entry(topFrame)
AnswerBox.grid(row=0,column=4)