#-*-coding:Utf-8-*-
from tkinter import *
import RPi.GPIO as GPIO
import time
import threading
#Paramétrage des noms pour GPIO
BUTTON1 = 18
BUTTON2 = 23
BUTTON3 = 24
BUTTON4 = 25
BUTTONreset = 16
BUTTONvalider = 20
LED1 = 4
LED2 = 17
LED3 = 27
LED4 = 22
LEDrouge = 5
LEDverte = 6
#Paramétrage des pins
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON1,GPIO.IN)
GPIO.setup(BUTTON2,GPIO.IN)
GPIO.setup(BUTTON3,GPIO.IN)
GPIO.setup(BUTTON4,GPIO.IN)
GPIO.setup(BUTTONreset,GPIO.IN)
GPIO.setup(BUTTONvalider,GPIO.IN)
GPIO.setup(LED1,GPIO.OUT)
GPIO.setup(LED2,GPIO.OUT)
GPIO.setup(LED3,GPIO.OUT)
GPIO.setup(LED4,GPIO.OUT)
GPIO.setup(LEDrouge,GPIO.OUT)
GPIO.setup(LEDverte,GPIO.OUT)
#On met à l’état 0 toutes les leds et les valeurs au préalable
GPIO.output(LED1, GPIO.LOW)
GPIO.output(LED2, GPIO.LOW)
GPIO.output(LED3, GPIO.LOW)
GPIO.output(LED4, GPIO.LOW)
GPIO.output(LEDrouge, GPIO.LOW)
GPIO.output(LEDverte, GPIO.LOW)
i=1
resultat=0
w=0
x=0
y=0
z=0
#import question/rep fichier txt
F = open('questionsréponses.txt','r')
Entete = F.readline().rstrip('\n\r').split(',')
#les index
Questionidx = Entete.index("Question")
SQuestionidx = Entete.index("sous-question")
Prop1idx = Entete.index("Prop1")
Prop2idx = Entete.index("Prop2")
Prop3idx = Entete.index("Prop3")
Prop4idx = Entete.index("Prop4")
widx = Entete.index("w")
xidx = Entete.index("x")
yidx = Entete.index("y")
zidx = Entete.index("z")
#mise en place des listes
Questionliste = []
SQuestionliste = []
Prop1liste = []
Prop2liste = []
Prop3liste = []
Prop4liste = []
wliste = []
xliste = []
yliste = []
zliste = []
for ligne in F :
donneesF = ligne.rstrip('\n\r').split(',')
Questionliste.append(donneesF[Questionidx])
SQuestionliste.append(donneesF[SQuestionidx])
Prop1liste.append(donneesF[Prop1idx])
Prop2liste.append(donneesF[Prop2idx])
Prop3liste.append(donneesF[Prop3idx])
Prop4liste.append(donneesF[Prop4idx])
wliste.append(donneesF[widx])
xliste.append(donneesF[xidx])
yliste.append(donneesF[yidx])
zliste.append(donneesF[zidx])
#Fonction interface questions
class TkinterThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global i
global Question
#lafenetre
Question = Tk()
Question.title("Question")
Question.geometry("800x650")
#Création Frame Image
Image = Frame(Question, bg="red", padx=20, pady=20, width = 800, height = 400)
Image.pack_propagate(False)
Image.pack()
#Création Frame QetR
QetR = Frame(Question, bg="black", padx=20, pady=20)
QetR.pack(fill="both", expand="yes", side = TOP)
#Image du Login Screen
Photo=PhotoImage(file="bank/Q"+str(i)+".gif")
ImageCanvas=Canvas(Image,width=800,height=400,bg="white")
ImageCanvas.create_image(0,0,image=Photo, anchor=NW)
ImageCanvas.pack(side=TOP)
#numéro de question
nQ = Label(QetR, text ="Question n°"+str(i), fg = 'white', bg = 'black',font="arial 17 bold")
nQ.pack(side = TOP, padx = 5, pady = 5)
#la question
Q11 = Label(QetR, text = Questionliste[i-1], fg = 'white', bg = 'black',font="arial 15 bold")
Q11.pack(side = TOP, padx = 0, pady = 0)
#proposition 1
A = Label(QetR, text = Prop1liste[i-1] ,fg = 'white', bg = 'black',font="arial 15")
A.pack(side = TOP, padx = 0, pady = 0)
#proposition 2
B = Label(QetR, text = Prop2liste[i-1], fg = 'white', bg = 'black', font='arial 15')
B.pack(side = TOP, padx = 0, pady = 0)
#Sous-question
if SQuestionliste[i-1] != '' :
Q12 = Label(QetR, text = SQuestionliste[i-1], fg='white' ,bg='black',font="arial 15 bold")
Q12.pack( padx = 0, pady = 0)
#proposition 3
C = Label(QetR, text = Prop3liste[i-1], fg = 'white', bg = 'black',font="arial 15")
C.pack(padx = 0, pady = 0)
#proposition 4
D = Label(QetR, text = Prop4liste[i-1], fg = 'white', bg = 'black', font='arial 15')
D.pack( padx = 0, pady = 0)
a=GPIO.input(BUTTON1)
Question.mainloop()
def stop(self):
self.running = False
def Destruction_Fen_Question():
Question.destroy()
#Class et Thread Rasp
class GPIOThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.running = False
def run(self):
self.running = True
global resultat
global i
global x
global y
global z
global w
while i<=20 and GPIO.input(BUTTON1)>=0 and GPIO.input(BUTTON2)>=0 and GPIO.input(BUTTON3)>=0 and GPIO.input(BUTTON4)>=0 :
GPIO.output(LEDrouge, GPIO.LOW)
GPIO.output(LEDverte, GPIO.LOW)
if GPIO.input(BUTTON1)==1 :
GPIO.output(LED1, GPIO.HIGH)
w=1
if GPIO.input(BUTTON2)==1 :
GPIO.output(LED2, GPIO.HIGH)
x=1
if GPIO.input(BUTTON3)==1 :
GPIO.output(LED3, GPIO.HIGH)
y=1
if GPIO.input(BUTTON4)==1 :
GPIO.output(LED4, GPIO.HIGH)
z=1
if GPIO.input(BUTTONreset)==1 :
GPIO.output(LED1, GPIO.LOW)
GPIO.output(LED2, GPIO.LOW)
GPIO.output(LED3, GPIO.LOW)
GPIO.output(LED4, GPIO.LOW)
w=0
x=0
y=0
z=0
if GPIO.input(BUTTONvalider)==1 :
while GPIO.input(BUTTONvalider)==1 :
time.sleep(1)
print (w,x,y,z)
print (wliste[i-1],xliste[i-1],yliste[i-1],zliste[i-1])
if w==wliste[i-1] and x==xliste[i-1] and y==yliste[i-1] and z==zliste[i-1] :
GPIO.output(LEDverte, GPIO.HIGH)
print ("Gagné !")
resultat=resultat+1
else :
GPIO.output(LEDrouge, GPIO.HIGH)
print ("Perdu !")
print (resultat)
w=0
x=0
y=0
z=0
time.sleep(3)
GPIO.input(BUTTON1)==0
GPIO.input(BUTTON2)==0
GPIO.input(BUTTON3)==0
GPIO.input(BUTTON4)==0
GPIO.output(LED1, GPIO.LOW)
GPIO.output(LED2, GPIO.LOW)
GPIO.output(LED3, GPIO.LOW)
GPIO.output(LED4, GPIO.LOW)
GPIO.output(LEDrouge, GPIO.LOW)
GPIO.output(LEDverte, GPIO.LOW)
Destruction_Fen_Question()
def stop(self):
self.running = False
ThreadRasp = GPIOThread()
ThreadTK = TkinterThread()
ThreadTK.start()
ThreadRasp.start()
ThreadTK.stop()
ThreadRasp.stop()