【问题标题】:Python: Tkinter stopping due to while loopPython:Tkinter 由于 while 循环而停止
【发布时间】:2015-08-28 15:57:48
【问题描述】:

我试图在我的程序中运行一个 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() 

【问题讨论】:

    标签: python loops while-loop tkinter


    【解决方案1】:

    我建议不要创建一个 while 循环,而是将 NewQuestion 设为一个函数。该函数最初被调用,然后如果答案正确,则再次调用该函数。这是我的函数代码,以及一个自动条目删除选项,以消除输入正确答案后退格的需要。

    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() 
    

    【讨论】:

      【解决方案2】:

      你有一个无限循环:

      while NewQuestion == True:
      

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

      还有:

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

      不起作用,因为grid 返回None。如果要保留对小部件的引用,则必须使用两行版本,例如:

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

      【讨论】:

        猜你喜欢
        • 2017-03-12
        • 1970-01-01
        • 2010-09-26
        • 2013-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多