tkinter
を使用してGUIを作成しようとしています。 「次の質問」を押すとラベルが更新されない
from tkinter import *
from random import randint
B3Questions = ["How is a cactus adapted to a desert environment?", "What factors could cause a species to become extinct?"]
B3Answers = ["It has leaves reduced to spines to cut water loss, a thick outer layer to cut down water loss and a deep-wide spreading root system to obtain as much water as possible", "Increased competition, new predators and new diseases"]
B3Possibles = [x for x in range (len(B3Questions))]
def loadGUI():
root = Tk() #Blank Window
questNum = generateAndCheck()
questionToPrint = StringVar()
answer = StringVar()
def showQuestion():
questionToPrint.set(B3Questions[questNum])
def showAnswer():
answer.set(B3Answers[questNum])
def reloadGUI():
global questNum
questNum = generateAndCheck()
return questNum
question = Label(root, textvariable = questionToPrint)
question.pack()
answerLabel = Label(root, textvariable = answer, wraplength = 400)
answerLabel.pack()
bottomFrame = Frame(root)
bottomFrame.pack()
revealAnswer = Button(bottomFrame, text="Reveal Answer", command=showAnswer)
revealAnswer.pack(side=LEFT)
nextQuestion = Button(bottomFrame, text="Next Question", command=reloadGUI)
nextQuestion.pack(side=LEFT)
showQuestion()
root.mainloop()
def generateAndCheck():
questNum = randint(0, 1)
print(questNum)
if questNum not in B3Possibles:
generateAndCheck()
else:
B3Possibles.remove(questNum)
return questNum
基本的には、質問のラベルが更新されません:これは私のコードです。 「次の質問」をもう一度押すと、コードがエラーの素晴らしいループに投げ込まれます。
私が間違っているつもりだところ、私は正直に見ることができないが、それが原因経験
'reloadGUI()'。 – RobertR
どうすればいいですか? –
次の質問を2回押した後に表示されるエラーは、数字のリストに何もないことです。したがって、あなたの関数 'reloadGUI'はPythonの再帰的制限に達するまで動作し続けます。 – Dzhao