【发布时间】: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