我正在尝试制作一个简单的数学游戏。现在我已经在互联网上进行了研究,并且已经工作了几个小时,但我似乎无法让这个简单的游戏工作。
游戏的运作方式: 游戏向用户询问问题,用户在条目栏中给出答案,用户点击按钮,用户收到另一个问题。
问题: 如何使其循环,因此每次用户回答问题时,循环/算法都会重新开始。因此,只有在单击按钮时,算法才能继续。
其他信息: 我使用Tkinter作为GUI。这是我的流程图: https://s31.postimg.org/pzs2m43kb/The_Math_Game_algorithm.png
我的问题是在GamePage下的while< 10循环。
import Tkinter as tk
import ttk as tkk
import random
TITLE_FONT = ("Helvetica", 18, "bold")
""" Functions """
""" GUI """
class App(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, GamePage, ValidationPage):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="TheMathGame", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
labelStart = tk.Label(self, text="Click start to continue",)
labelStart.pack(side="top", fill="x", pady=10)
button1 = tkk.Button(self, text="Start", command = lambda: controller.show_frame("GamePage"))
button1.pack()
class GamePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.count = 0
self.price = 0
self.label = tk.Label(self, text='Answer the question', font=TITLE_FONT)
self.label.pack(side="top", fill="x", pady=10)
while self.count < 10:
MathQuestion, MathAnswer = self.mathGenerator()
self.label2 = tk.Label(self, text=MathQuestion)
self.label2.pack(side="top", fill="x", pady=10)
userInputField = tk.Entry(self)
userInputField.pack()
buttonNext = tk.Button(self, text="OK", command=lambda: self.validationManager(MathAnswer, userInputField))
buttonNext.pack()
def mathGenerator(self):
def answerMath(number1, number2, function):
if function == "+":
return number1 + number2
if function == "-":
return number1 - number2
if function == "*":
return number1 * number2
if function == "/":
return number1 / number2
def numberGenerator():
number1 = random.randint(1, 100)
number2 = random.randint(1, 100)
return (number1, number2)
def subtractFunction(math_function):
number1, number2 = numberGenerator()
while number1 < number2:
number1, number2 = numberGenerator()
else:
question = ("%d %s %d" % (number1, math_function, number2))
answer = answerMath(number1, number2, math_function)
return (question), (answer)
def divideFunction(math_function):
number1, number2 = numberGenerator()
while number1 % number2 != 0 or number1 < number2:
number1, number2 = numberGenerator()
else:
question = ("%d %s %d" % (number1, math_function, number2))
answer = answerMath(number1, number2, math_function)
return (question), (answer)
math_functions = ['+', '-', '*', '/']
math_function = (random.choice(math_functions))
if math_function == '/':
(question), (answer) = divideFunction(math_function)
elif math_function == '-':
(question), (answer) = subtractFunction(math_function)
else:
number1, number2 = numberGenerator()
if math_function == '*':
math_function_string = 'x'
else:
math_function_string = math_function
question = ("%d %s %d" % (number1, math_function_string, number2))
answer = answerMath(number1, number2, math_function)
return question, answer
def validationManager(self, questionAnswer, userAnswer):
questionAnswer = questionAnswer
userAnswerd = userAnswer.get()
if int(userAnswerd) == questionAnswer:
return "Correct"
else:
return "Wrong"
class ValidationPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.labelText = 'Wrong..'
self.ValidationLabel = tk.Label(self, text=self.labelText, font=TITLE_FONT)
self.ValidationLabel.pack(side="top", fill="x", pady=10)
if __name__ == "__main__":
root = App()
root.iconbitmap('Tesla.ico')
root.resizable(width=False, height=False)
root.geometry('{}x{}'.format(350, 250))
root.title("TheMathGame")
photo = tk.PhotoImage(file="monkey.gif")
label = tk.Label(root, image=photo)
label.pack()
root.mainloop()
答案 0 :(得分:0)
tkinter的一个大问题是,虽然需要用户进行交互以退出的循环会导致它崩溃。
以下是我的调整。
class GamePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.count = 0
self.price = 0
self.label = tk.Label(self, text='Answer the question', font=TITLE_FONT)
self.label.pack(side="top", fill="x", pady=10)
self.label2 = tk.Label(self)
self.label2.pack(side="top", fill="x", pady=10)
self.userInputField = tk.Entry(self)
self.userInputField.pack()
buttonNext = tk.Button(self, text="OK", command=self.validationManager)
buttonNext.pack()
self.askQuestion()
def askQuestion(self):
self.userInputField.delete(0, tk.END)
MathQuestion, self.MathAnswer = self.mathGenerator()
self.label2.config(text = MathQuestion)
def validationManager(self):
if int(self.userInputField.get()) == self.MathAnswer:
self.price += 1
self.count += 1
print(self.count, self.price)
if self.count == 10:
print("To the price page")
else:
self.askQuestion()
你也试图从按钮点击调用的函数返回一个值,这真的不起作用。最好让被调用的函数执行一个动作,这意味着在按下按钮时应该执行所有操作。
那些不完整的内容,您需要使用它来显示&#34;正确&#34;,&#34;不正确&#34;还有别的。但这应该可以解决错误的问题。