好的,所以主要问题如下:我无法突然从第三帧移动到第四帧;当我的按钮调用命令时,它们不会保存名称或检查答案是否正确,任何帮助将不胜感激
from tkinter import *
import random
import sys
#Question class, list of questions, and method to ask and choose the questions
class Question():
def __init__(self,question,answer,options):
self.question = question
self.answer = answer
self.options = options
def ask(self):
print (self.question + "?")
for n, option in enumerate(self.options):
print ("%d) %s" % (n + 1, options))
response = int(sys.stdin.readline().strip()) # answers are integers
if response == self.answer:
print ("CORRECT")
else:
print ("wrong")
questions = [
Question("what is a group of crows called",1,["murder","the night's watch", "school", "flock"]),
Question("What is the square root of a potato",4,["does not exist","b)Half a potato"," c)Two equal portions that together add up to half the potato"," d)9"]),
Question("What is the name of the owner of the white wand in Harry Potter",2,["a)Harry"," b)Voldemort ","c)Snape ","d)Ron"]),
Question("How fast is a cheetah",2,["a)very fast"," b)ultra fast"," c)fast"," d)not as fast as the author of this game"]),
Question("How old must Spongebob be",4,[" a)9"," b)16"," c)58"," d)18"]),
Question("the best type of art is",3,[" a)classic"," b)french"," c)waveform"," d)electronic"]),
Question("the best sculputres are made out of",1,[" a)styrofoam"," b)chloroform"," c)metal"," d)clay"]),
Question("the best basketball player in the world is",3,[" a)chef curry"," b)stephanie cuurry"," c)Like Mike"," d)Paul George"]),
Question("the best soccer player in the world is",1,[" a)Harry Kane ","b)Shoalin Soccer Protaganist"," c)Neymar ","d)Rooney"]),
Question("which of the following people is an EGOT winner",1,[" a)whoopie goldberg"," b)neil patrick harris"," c)Tracy jordan"," d)Dule Hill"]),
Question("how many sides are on an egyptian pyramid",3,[" a)4"," b)5"," c)3000"," d)100"]),
Question("who is the real hero of the karate kid",4,[" a)ralph machio"," b)mr miyagi"," c)the tiger guy who almost beats ralph"," d)danny sans mom"]),
Question("which was not a best picture winner",2,[" a)birdman"," b)dark knight"," c)gladiator"," d)hurt locker"]),
Question("the most common surname is",3,[" a)smith"," b)mohamed"," c)Lee"," d)miller"]),
Question("is it a good choice to take APES",4,[" a)yes its easy"," b)no its stupid ","c)yes its very interesting"," d)no because in one year the dark overlord khatulu may wreak havoc on all environments"]),
]
random.shuffle(questions)
#for question in questions:
# question.ask()
########GUI##############
def setName():
user1 = userOneName.get()
user2 = userTwoName.get()
def raise_frame(frame):
frame.tkraise()
def combine_funcs(*funcs):
def combined_func(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return 1
return combined_func
def checkAnswerUser1(intVariable, number):
if (intVariable == questions[number].answer):
user1Score = user1Score + 1
def checkAnswerUser2(intVariable, number):
if (intVariable == questions[number].answer):
user2Score = user2Score + 1
def resetGame():
userOneName = ""
userTwoName = ""
user1Score = 0
user2Score = 0
random.shuffle(questions)
root = Tk()
f1 = Frame(root)
f2 = Frame(root)
f3 = Frame(root)
f4 = Frame(root)
f5 = Frame(root)
f6 = Frame(root)
f7 = Frame(root)
f8 = Frame(root)
f9 = Frame(root)
f10 = Frame(root)
f11 = Frame(root)
f12 = Frame(root)
for frame in (f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12):
frame.grid(row=0, column=0, sticky='news')
### First Frame
#Labels and Entry boxes for user names
##content = StringVar()
##content2 = StringVar()
userOneLabel = Label(f1, text="User 1: Enter your name:")
userOneName = Entry(f1)
userTwoLabel = Label(f1, text="User 2: Enter your name:")
userTwoName = Entry(f1)
userOneLabel.pack(fill=X)
userOneName.pack(fill=X)
userTwoLabel.pack(fill=X)
userTwoName.pack(fill=X)
user1 = userOneName.get()
user2 = userTwoName.get()
user1Score = 0
user2Score = 0
##Next Button
NextButton = Button(f1, text='Next ---->', command=combine_funcs(lambda:raise_frame(f2),setName()))
NextButton.pack(side=RIGHT)
v = IntVar()
###Second Frame
questionPrinter1 = Label(f2, text= user1 + questions[0].question)
questionPrinter1.pack()
Radiobutton(f2, text=questions[0].options[0], variable=v, value=1).pack(side=TOP)
Radiobutton(f2, text=questions[0].options[1], variable=v, value=2).pack(side=TOP)
Radiobutton(f2, text=questions[0].options[2], variable=v, value=3).pack(side=TOP)
Radiobutton(f2, text=questions[0].options[3], variable=v, value=4).pack(side=TOP)
#When button is clicked, if the correct radio button is clicked, add 1 to the score
#add the same frame except change the user name
#do the choose random function every time the button is clicked
NextButton1 = Button(f2, text='Next ---->', command=combine_funcs(lambda:raise_frame(f3), checkAnswerUser1(v,0)))
NextButton1.pack(side=RIGHT)
###Third Frame
j = IntVar()
questionPrinter2 = Label(f3, text= (user1 + questions[1].question))
questionPrinter2.pack()
Radiobutton(f3, text=questions[1].options[0], variable=j, value=1).pack(side=TOP)
Radiobutton(f3, text=questions[1].options[1], variable=j, value=2).pack(side=TOP)
Radiobutton(f3, text=questions[1].options[2], variable=j, value=3).pack(side=TOP)
Radiobutton(f3, text=questions[1].options[3], variable=j, value=4).pack(side=TOP)
NextButton2 = Button(f3, text='Next ------>', command=(lambda:raise_frame(f4),checkAnswerUser2(j,1)))
NextButton2.pack(side=RIGHT)
###Fourth Frame
questionPrinter3 = Label(f4, text= user1 + questions[2].question)
questionPrinter3.pack()
k = IntVar()
Radiobutton(f4, text=questions[2].options[0], variable=k, value=1).pack(side=TOP)
Radiobutton(f4, text=questions[2].options[1], variable=k, value=2).pack(side=TOP)
Radiobutton(f4, text=questions[2].options[2], variable=k, value=3).pack(side=TOP)
Radiobutton(f4, text=questions[2].options[3], variable=k, value=4).pack(side=TOP)
NextButton3 = Button(f4, text='Next ---->', command=(lambda:raise_frame(f5),checkAnswerUser1(k,2)))
NextButton3.pack(side=RIGHT)
### Fifth Frame
questionPrinter3 = Label(f5, text= user1 + questions[3].question)
questionPrinter3.pack()
a = IntVar()
Radiobutton(f5, text=questions[3].options[0], variable=a, value=1).pack(side=TOP)
Radiobutton(f5, text=questions[3].options[1], variable=a, value=2).pack(side=TOP)
Radiobutton(f5, text=questions[3].options[2], variable=a, value=3).pack(side=TOP)
Radiobutton(f5, text=questions[3].options[3], variable=a, value=4).pack(side=TOP)
NextButton3 = Button(f5, text='Next ---->', command=(lambda:raise_frame(f6),checkAnswerUser2(a,3)))
NextButton3.pack(side=RIGHT)
###Sixth Frame
questionPrinter3 = Label(f6, text= user1 + questions[4].question)
questionPrinter3.pack()
b = IntVar()
Radiobutton(f6, text=questions[4].options[0], variable=b, value=1).pack(side=TOP)
Radiobutton(f6, text=questions[4].options[1], variable=b, value=2).pack(side=TOP)
Radiobutton(f6, text=questions[4].options[2], variable=b, value=3).pack(side=TOP)
Radiobutton(f6, text=questions[4].options[3], variable=b, value=4).pack(side=TOP)
NextButton3 = Button(f6, text='Next ---->', command=(lambda:raise_frame(f7),checkAnswerUser1(b,4)))
NextButton3.pack(side=RIGHT)
###7th Frame
questionPrinter3 = Label(f7, text= user1 + questions[5].question)
questionPrinter3.pack()
c = IntVar()
Radiobutton(f7, text=questions[5].options[0], variable=c, value=1).pack(side=TOP)
Radiobutton(f7, text=questions[5].options[1], variable=c, value=2).pack(side=TOP)
Radiobutton(f7, text=questions[5].options[2], variable=c, value=3).pack(side=TOP)
Radiobutton(f7, text=questions[5].options[3], variable=c, value=4).pack(side=TOP)
NextButton3 = Button(f7, text='Next --->', command=(lambda:raise_frame(f8),checkAnswerUser2(c,5)))
NextButton3.pack(side=RIGHT)
###8th Frame
questionPrinter3 = Label(f8, text= user1 + questions[6].question)
questionPrinter3.pack()
d = IntVar()
Radiobutton(f8, text=questions[6].options[0], variable=d, value=1).pack(side=TOP)
Radiobutton(f8, text=questions[6].options[1], variable=d, value=2).pack(side=TOP)
Radiobutton(f8, text=questions[6].options[2], variable=d, value=3).pack(side=TOP)
Radiobutton(f8, text=questions[6].options[3], variable=d, value=4).pack(side=TOP)
NextButton3 = Button(f8, text='Next --->', command=(lambda:raise_frame(f9),checkAnswerUser1(d,6)))
NextButton3.pack(side=RIGHT)
###9th Frame
questionPrinter3 = Label(f9, text= user1 + questions[7].question)
questionPrinter3.pack()
e = IntVar()
Radiobutton(f9, text=questions[7].options[0], variable=e, value=1).pack(side=TOP)
Radiobutton(f9, text=questions[7].options[1], variable=e, value=2).pack(side=TOP)
Radiobutton(f9, text=questions[7].options[2], variable=e, value=3).pack(side=TOP)
Radiobutton(f9, text=questions[7].options[3], variable=e, value=4).pack(side=TOP)
NextButton3 = Button(f9, text='Next --->', command=(lambda:raise_frame(f10),checkAnswerUser2(e,7)))
NextButton3.pack(side=RIGHT)
##10th Frame
questionPrinter3 = Label(f10, text= user1 + questions[8].question)
questionPrinter3.pack()
f = IntVar()
Radiobutton(f10, text=questions[8].options[0], variable=f, value=1).pack(side=TOP)
Radiobutton(f10, text=questions[8].options[1], variable=f, value=2).pack(side=TOP)
Radiobutton(f10, text=questions[8].options[2], variable=f, value=3).pack(side=TOP)
Radiobutton(f10, text=questions[8].options[3], variable=f, value=4).pack(side=TOP)
NextButton3 = Button(f10, text='Next --->', command=(lambda:raise_frame(f11),checkAnswerUser1(f,8)))
NextButton3.pack(side=RIGHT)
##11th Frame
questionPrinter3 = Label(f11, text= user1 + questions[9].question)
questionPrinter3.pack()
g = IntVar()
Radiobutton(f11, text=questions[9].options[0], variable=g, value=1).pack(side=TOP)
Radiobutton(f11, text=questions[9].options[1], variable=g, value=2).pack(side=TOP)
Radiobutton(f11, text=questions[9].options[2], variable=g, value=3).pack(side=TOP)
Radiobutton(f11, text=questions[9].options[3], variable=g, value=4).pack(side=TOP)
NextButton3 = Button(f11, text='Next --->', command=(lambda:raise_frame(f12),checkAnswerUser2(g,9)))
NextButton3.pack(side=RIGHT)
## 12th and final frame
User1ScorePrinter = Label(f12, text= user1 + "'s score is:" + str(user1Score))
User1ScorePrinter.pack()
User2ScorePrinter = Label(f12, text= user2 + "'s score is:" + str(user1Score))
User2ScorePrinter.pack()
User1Winner = Label(f12, text = user1 + "is the winner")
User2Winner = Label(f12, text = user2 + "is the winner")
if(user1Score > user2Score):
User1Winner.pack()
else:
User2Winner.pack()
####when the game is reset, the user names should be reset, the questions should be shuffled, and the scores should be reset
NextButton3 = Button(f12, text='Restart the Game', command=combine_funcs(lambda:raise_frame(f1),resetGame()))
NextButton3.pack()
raise_frame(f1)
root.mainloop()
你不需要 12 帧来解决这个问题。 您可以展开 Question 类来处理与一组问题关联的所有内容,这正是类结构的作用。 请注意,这遵循您的代码,但快速而肮脏只是为了显示它是如何完成的,因此您可以并且应该进行改进。
class Question():
def __init__(self, root, questions):
self.fr=Frame(root)
self.fr.pack(side="top")
self.questions = questions
self.correct=False
self.ask()
def ask(self):
print (self.questions[0] + "?")
## [0]=question, [1]=correct answer
Label(self.fr, text=self.questions[0]+"?",
bg="lightyellow").pack()
self.options=self.questions[2]
self.correct_answer=self.options[self.questions[1]-1]
self.v=IntVar()
self.v.set(0)
## allow for options of different lengths
for ctr in range(len(self.options)):
b=Radiobutton(self.fr, text=self.options[ctr], variable=self.v, value=ctr,
command=self.check_answer)
b.pack()
self.result=Label(self.fr, text="", bg="lightblue")
self.result.pack()
def check_answer(self):
self.correct=False
number=self.v.get()
if (self.correct_answer == self.options[number]):
self.correct=True
self.result["text"]="Correct"
else:
self.result["text"]="Wrong"
## while testing
print self.correct, number, self.options[number]
class AskAllQuestions():
def __init__(self, root):
self.root=root
self.this_question=0
self.total_correct=0
self.TQ=None
self.next_but=None
self.shuffle_questions()
self.next_question()
def next_question(self):
""" self.this_question is a number that increments on each pass
self.question_order is a list of numbers in random order
so self.next_question picks the self.this_question number
from the random order self.question_order list
"""
if self.TQ: ## not first question
if self.TQ.correct:
self.total_correct += 1
print "Total Correct", self.total_correct
self.TQ.fr.destroy()
self.next_q_fr.destroy()
if self.this_question < len(self.question_order):
this_question_num=self.question_order[self.this_question]
self.this_question += 1
self.next_q_fr=Frame(self.root)
self.next_q_fr.pack(side="bottom")
self.next_but=Button(self.next_q_fr, text='Next ---->', bg="orange",
command=self.next_question)
self.next_but.pack(side="top")
Button(self.next_q_fr, text="End Program", command=root.quit,
bg="red").pack(side="bottom")
self.TQ=Question(self.root, self.all_questions[this_question_num])
else:
self.root.quit()
def shuffle_questions(self):
self.all_questions = [
("what is a group of crows called",1,["murder","the night's watch", "school", "flock"]),
("What is the square root of a potato",4,["does not exist","b)Half a potato"," c)Two equal portions that together add up to half the potato"," d)9"]),
("What is the name of the owner of the white wand in Harry Potter",2,["a)Harry"," b)Voldemort ","c)Snape ","d)Ron"]),
("How fast is a cheetah",2,["a)very fast"," b)ultra fast"," c)fast"," d)not as fast as the author of this game"]),
("How old must Spongebob be",4,[" a)9"," b)16"," c)58"," d)18"]),
("the best type of art is",3,[" a)classic"," b)french"," c)waveform"," d)electronic"]),
("the best sculputres are made out of",1,[" a)styrofoam"," b)chloroform"," c)metal"," d)clay"]),
("the best basketball player in the world is",3,[" a)chef curry"," b)stephanie cuurry"," c)Like Mike"," d)Paul George"]),
("the best soccer player in the world is",1,[" a)Harry Kane ","b)Shoalin Soccer Protaganist"," c)Neymar ","d)Rooney"]),
("which of the following people is an EGOT winner",1,[" a)whoopie goldberg"," b)neil patrick harris"," c)Tracy jordan"," d)Dule Hill"]),
("how many sides are on an egyptian pyramid",3,[" a)4"," b)5"," c)3000"," d)100"]),
("who is the real hero of the karate kid",4,[" a)ralph machio"," b)mr miyagi"," c)the tiger guy who almost beats ralph"," d)danny sans mom"]),
("which was not a best picture winner",2,[" a)birdman"," b)dark knight"," c)gladiator"," d)hurt locker"]),
("the most common surname is",3,[" a)smith"," b)mohamed"," c)Lee"," d)miller"]),
("is it a good choice to take APES",4,[" a)yes its easy"," b)no its stupid ","c)yes its very interesting"," d)no because in one year the dark overlord khatulu may wreak havoc on all environments"]),
]
self.question_order=range(len(self.all_questions))
random.shuffle(self.question_order)
root=Tk()
AQ=AskAllQuestions(root)
root.mainloop()