CONNEXION
  • RetourJeux
    • Sorties
    • Hit Parade
    • Les + populaires
    • Les + attendus
    • Soluces
    • Tous les Jeux
    • Gaming
  • RetourActu Gaming
    • News
    • Astuces
    • Tests
    • Previews
    • Toute l'actu gaming
  • RetourBons plans
    • Bons plans
    • Bons plans Smartphone
    • Bons plans Hardware
    • Bons plans Image et Son
    • Bons plans Amazon
    • Bons plans Cdiscount
    • Bons plans Decathlon
    • Bons plans Fnac
    • Tous les Bons plans
  • RetourJVTech
    • Actus High-Tech
    • Intelligence Artificielle
    • Smartphones
    • Mobilité urbaine
    • Hardware
    • Image et son
    • Tutoriels
    • Tests produits High-Tech
    • Guides d'achat High-Tech
    • JVTech
  • RetourCulture
    • Actus Culture
    • Culture
  • RetourVidéos
    • A la une
    • Gaming Live
    • Vidéos Tests
    • Vidéos Previews
    • Gameplay
    • Trailers
    • Chroniques
    • Replay Web TV
    • Toutes les vidéos
  • RetourForums
    • Hardware PC
    • PS5
    • Switch 2
    • Xbox Series
    • Switch
    • Pokemon pocket
    • FC 25 Ultimate Team
    • League of Legends
    • Tous les Forums
  • PC
  • PS5
  • Xbox Series
  • Switch 2
  • PS4
  • One
  • Switch
  • iOS
  • Android
  • MMO
  • RPG
  • FPS
En ce moment Genshin Impact Valhalla Breath of the wild Animal Crossing GTA 5 Red dead 2
Liste des sujets

[Python/Tkinter] Rafraîchir une fenêtre avec une nouvelle variable

AxlRoses19
AxlRoses19
Niveau 2
17 avril 2017 à 10:16:22

Bonjour,
Dans le cadre d'un projet en informatique, je souhaiterais programmer l'épreuve du code de la route à l'aide du langage Python.
J'ai réussi à faire fonctionner mon programme pour une seule question, cependant je voudrais répéter cette opération 20 fois...
J'utilise des Thread car j'utilise une raspberry en même temps, donc l'utilisation de boucle est donc impossible (enfin je pense, mais je ne suis pas sur ^^).
Je voudrais donc savoir comment faire pour que lorsque j'appuie sur un bouton, la fenêtre avec la question se rafraîchisse avec une variable qui prend +1, donc avec des changements (question; image, réponse attendue...).

Voici mon code qui fonctionne pour 1 seule question :

#-*-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()

Merci d'avance pour vos réponses ^^

Message édité le 17 avril 2017 à 10:18:03 par AxlRoses19
AxlRoses19
AxlRoses19
Niveau 2
18 avril 2017 à 15:34:00

Svp c'est urgent :/
Je suis vraiment bloqué
A l'aide

AxlRoses19
AxlRoses19
Niveau 2
19 avril 2017 à 08:55:49

up

Sous forums
  • Aide à l'achat Mac
  • Création de Jeux
  • Linux
  • Création de sites web
  • Programmation
  • Internet
  • Steam Deck
  • Macintosh
  • Hardware
La vidéo du moment