【问题标题】:Quiz game : python tkinter wait for click on button to continue execute the code before quitting问答游戏:python tkinter 等待点击按钮继续执行代码,然后退出
【发布时间】:2017-10-23 06:02:57
【问题描述】:

这学期我参加了网络编程课,我的项目是一个问答游戏,有一个简单的图形界面。

我想显示问题,就像玩家点击一个按钮来回答一样。

问题是,我不知道如何在python 中编写"wait for click",代码一直执行到最后。

我尝试在 boolean 条件下使用一段时间(如果没有点击则等待),这正在等待我想要的,但没有显示图形界面...

这是我的代码,我有点绝望

提前感谢您的帮助。

import numpy as np
from numpy.random import shuffle
from random import randint
import socket
import time
from Tkinter import *
from time import clock


class Interface(Frame):
    def __init__(self, fenetre, **kwargs):
        Frame.__init__(self, fenetre, width=768, height=576, **kwargs)
        self.pack(fill=BOTH)


        #DATAS
        self.question = ["What is the capital of France?","In which continent is Argentina?","Where is Big Ben?","What is the most densely populated country?","What language do they speak in Brazil?"]
        self.answer = [["Paris","London","Berlin","Madrid"],["South America","Africa","Europe","Asia"],["London","New York","Mexico","Jakarta"],["China","India","USA","Indonesia"],["Portuguese","Spanish","French","English"]]

        self.question_done=[0]*(len(self.question))
        #SCORE, stored as a list score[0]--> score of the player 1
        self.score=[0]


        # Creation of widgets
        self.message = Label(self, text="Welcome to the Bule Game")
        self.message.pack()

        self.bouton_quitter = Button(self, text="Quitter", command=self.quit)
        self.bouton_quitter.pack(side="bottom")


        #Total number of questions
        self.totalnb = 3


        self.bouton_a1 = Button(self, text="",command = self.checkAnswer)
        self.bouton_a1.pack(side="left")
        self.bouton_a2 = Button(self, text="",command = self.checkAnswer)
        self.bouton_a2.pack(side="left")
        self.bouton_a3 = Button(self, text="",command = self.checkAnswer)
        self.bouton_a3.pack(side="left")
        self.bouton_a4 = Button(self, text="",command = self.checkAnswer)
        self.bouton_a4.pack(side="left")




        for i in range(self.totalnb):
            #Choose the question
            self.nbq = self.chooseQuestion(self.question)
            print("the number is {}".format(self.nbq))
            self.message["text"] = self.question[self.nbq]            
            #answers possible
            self.ans=self.displayA(self.question,self.answer,self.nbq)            
            self.play()



    def play(self):
            #update buttons
            self.bouton_a1["text"]=self.ans[0]
            self.bouton_a1["command"]=(lambda: self.checkAnswer(self.answer,self.ans[0],self.nbq,self.score))

            self.bouton_a2["text"]=self.ans[1]
            self.bouton_a2["command"]=(lambda: self.checkAnswer(self.answer,self.ans[1],self.nbq,self.score))

            self.bouton_a3["text"]=self.ans[2]
            self.bouton_a3["command"]=(lambda: self.checkAnswer(self.answer,self.ans[2],self.nbq,self.score))

            self.bouton_a4["text"]=self.ans[3]
            self.bouton_a4["command"]=(lambda: self.checkAnswer(self.answer,self.ans[3],self.nbq,self.score))





    #CHOOSE RANDOMLY A QUESTION IN THE LIST
    def chooseQuestion(self,question):
        k = randint(0,len(question)-1)
        if (self.question_done[k]!=0):
            while(self.question_done[k]!=0):
                k = randint(0,len(question)-1)
            self.question_done[k]=1
        else :
            self.question_done[k]=1
        #print(question[k])
        #displayA(question,answer,k)
        #print("le num interne est {} ".format(k))
        return k


    #SHOW THE POSSIBLE ANSWERS
    def displayA(self,question,answer,i):
        a = answer[i]
        order = np.arange(4)
        shuffle(order) #create list from 1 to 4 in different order --> to print the answers in random order
        a_display = [a[order[0]],a[order[1]],a[order[2]],a[order[3]]]
        return a_display

    #CHECK IF GOOD ANSWER OR NOT
    def checkAnswer(self,answer,agiven,qnb,score):
        print("CHECK")

        test = False
        if(answer[qnb][0] in agiven):
            test = True
            score[0]=score[0]+1
        print("the answer is {}".format(test))
        return test

    def quit(self):
        self.message["text"] = "The score is {}.".format(self.score)




fenetre = Tk()
interface = Interface(fenetre)
interface.mainloop()

【问题讨论】:

    标签: python python-2.7 button tkinter wait


    【解决方案1】:

    如果您希望程序等待按钮单击,那么您应该将下一个要执行的代码放在按钮回调命令中。我将问题询问代码移动到一个名为nextQuestion 的方法中,在满足条件后在checkAnswer 中调用该方法。我还用足够多的 cmets 进行了一些小的改进来解释它们(我认为)。

    完整代码如下:

    import numpy as np
    from numpy.random import shuffle
    from random import randint
    import socket
    import time
    from Tkinter import *
    from time import clock
    
    
    class Interface(Frame):
        def __init__(self, fenetre, **kwargs):
            Frame.__init__(self, fenetre, width=768, height=576, **kwargs)
            self.pack(fill=BOTH)
    
            #DATAS
            self.question = ["What is the capital of France?","In which continent is Argentina?","Where is Big Ben?","What is the most densely populated country?","What language do they speak in Brazil?"]
            self.answer = [["Paris","London","Berlin","Madrid"],["South America","Africa","Europe","Asia"],["London","New York","Mexico","Jakarta"],["China","India","USA","Indonesia"],["Portuguese","Spanish","French","English"]]
    
            self.question_done=[0]*(len(self.question))
            #SCORE, stored as a list score[0]--> score of the player 1
            self.score=[0]
    
            # Creation of widgets
            self.message = Label(self, text="Welcome to the Bule Game")
            self.message.pack()
    
            self.bouton_quitter = Button(self, text="Quitter", command=self.quit)
            self.bouton_quitter.pack(side="bottom")
    
            self.bouton_start = Button(self, text="Start", command=self.startQestion)
            self.bouton_start.pack()
    
    
            #Total number of questions
            self.totalnb = 3
    
            # Variable to keep track of how many questions have been asked
            self.questions_asked = 1 
    
    
        def startQestion(self):
            # Create buttons before you ask the first questions
            self.bouton_a1 = Button(self, text="   ",command = self.checkAnswer)
            self.bouton_a1.pack(side="left")
            self.bouton_a2 = Button(self, text="   ",command = self.checkAnswer)
            self.bouton_a2.pack(side="left")
            self.bouton_a3 = Button(self, text="   ",command = self.checkAnswer)
            self.bouton_a3.pack(side="left")
            self.bouton_a4 = Button(self, text="   ",command = self.checkAnswer)
            self.bouton_a4.pack(side="left")
    
            self.bouton_start.pack_forget() #Remove the start button
            self.nextQuestion() # ask question
    
    
        def nextQuestion(self):
            #Choose the question
            self.nbq = self.chooseQuestion(self.question)
            print("the number is {}".format(self.nbq))
            self.message["text"] = self.question[self.nbq]            
            #answers possible
            self.ans=self.displayA(self.question,self.answer,self.nbq)     
            self.play()
    
    
        def play(self):
                #update buttons
                self.bouton_a1["text"]=self.ans[0]
                self.bouton_a1["command"]=(lambda: self.checkAnswer(self.answer,self.ans[0],self.nbq,self.score))
    
                self.bouton_a2["text"]=self.ans[1]
                self.bouton_a2["command"]=(lambda: self.checkAnswer(self.answer,self.ans[1],self.nbq,self.score))
    
                self.bouton_a3["text"]=self.ans[2]
                self.bouton_a3["command"]=(lambda: self.checkAnswer(self.answer,self.ans[2],self.nbq,self.score))
    
                self.bouton_a4["text"]=self.ans[3]
                self.bouton_a4["command"]=(lambda: self.checkAnswer(self.answer,self.ans[3],self.nbq,self.score))
    
    
    
        #CHOOSE RANDOMLY A QUESTION IN THE LIST
        def chooseQuestion(self,question):
            k = randint(0,len(question)-1)
            if (self.question_done[k]!=0):
                while(self.question_done[k]!=0):
                    k = randint(0,len(question)-1)
                self.question_done[k]=1
            else :
                self.question_done[k]=1
            #print(question[k])
            #displayA(question,answer,k)
            #print("le num interne est {} ".format(k))
            return k
    
    
        #SHOW THE POSSIBLE ANSWERS
        def displayA(self,question,answer,i):
            a = answer[i]
            order = np.arange(4)
            shuffle(order) #create list from 1 to 4 in different order --> to print the answers in random order
            a_display = [a[order[0]],a[order[1]],a[order[2]],a[order[3]]]
            return a_display
    
        #CHECK IF GOOD ANSWER OR NOT
        def checkAnswer(self,answer,agiven,qnb,score):
            print("CHECK")
    
            test = False
            if(answer[qnb][0] in agiven):
                test = True
                score[0]=score[0]+1
            print("the answer is {}".format(test))
            #Check to see if the maximum number of questions has been asked already
            if self.questions_asked < self.totalnb:
                self.nextQuestion() # Ask the next question if less number of questions has been asked
                self.questions_asked += 1  # Update the number of questions that has been asked
    
            # If maximum number of questions is asked, display end message and remove answer buttons
            else:
                self.message["text"] = "End of Qestion"
                self.bouton_a1.pack_forget()
                self.bouton_a2.pack_forget()
                self.bouton_a3.pack_forget()
                self.bouton_a4.pack_forget()
            #return test
    
        def quit(self):
            self.message["text"] = "The score is {}.".format(self.score)
    
    
    
    fenetre = Tk()
    interface = Interface(fenetre)
    interface.mainloop()
    

    【讨论】:

    • 非常感谢,这正是我所需要的 =) 我理解您所做的所有改进!
    猜你喜欢
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 2020-09-30
    • 2015-10-04
    • 2020-06-14
    • 2016-09-13
    • 2014-12-14
    • 1970-01-01
    相关资源
    最近更新 更多