Salut à tous !
J'ai décidé de passer aux grands moyens et de venir consulter Le Forum face à un souci qui me fait tourner la tête.

Intro:
C'est un script Python tournant sur Raspberry Pi A+ construit autour du GUI Tkinter et qui permet via 8 boutons différents de faire 8 actions différentes (incroyable ).
Nom : projet.png
Affichages : 318
Taille : 9,4 Ko

Symptômes:
Au lancement du script, la fenêtre se construit correctement et le code s'execute entièrement.
Un peu trop d'ailleurs car je m'attendais à ce que le "command" liés aux boutons ne se déclenchent que lors du Clic sur le bouton.
Au contraire, ils se lancent au démarrage du script sans y avoir cliqu dessus
Cela a pour conséquence le lancement de tous les Thread en simultané et le Freeze de l'application

Plus:
J'aimerais également votre avis sur les points suivants:
• Afficher une fenêtre Tkinter via SSH
J'accède à mon RPi via SSH. La nécessité d'ajouter la balise suivante est la seule façon que j'ai trouvée pour arriver à afficher une fenêtre Tkinter sur mon poste client (Windows)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
os.system("sudo cp ~pi/.Xauthority ~root/")
J'imagine que le problème sous-jacent à cela est qu'à chaque lancement du script il réécrira ces mêmes valeurs au même endroit tant qu'un Reboot ne sera pas effectué
Des suggestions quand à son utilisation ou à une quelconque alternative?

• Utilisation du module "os" pour l'appel de commandes externes
Je compte lancer un Streaming à partir de la caméra du RPi. Pour cela, je dois effectuer une commande externe.
Est-ce une bonne pratique ? J'ai déjà exploré le module "subprocess" mais sans succès à ce jour.

• La fonction "dispatchCmd" est-elle de trop ?
Passer par la commande suivante ne serait pas moins verbeux?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
command=Thread(target=loop1_5).start()
Merci d'avance pour votre clairvoyance
Voici le code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/python
# coding: utf-8
 
############################
# IMPORTATION DES LIBRAIRIES
import os
import time
from threading import Thread
import RPi.GPIO as GPIO
# GUI
from Tkinter import *
import tkFont
import tkMessageBox
 
os.system("sudo cp ~pi/.Xauthority ~root/")
 
###########################
# DÉCLARATION DES FONCTIONS
def dispatchCmd(arg):
	if   arg == 1:
	    #Eclairage
		Thread(target=lightUp).start()
	elif arg == 2:
	    #Prise de vue
		Thread(target=loop1_5).start()
	elif arg == 3:
	    #Bell
		Thread(target=loop6_10).start()
	elif arg == 4:
	    #Lancement du Stream
		Thread(target=loop11_15).start()
	elif arg == 5:
	    #Zoom numérique
		Thread(target=loop16_20).start()
	elif arg == 6:
	    #Lancement du radar à ultrasons
		Thread(target=loop1_5).start()
	else:
		print("ERROR: UNKNOWN METHOD")
 
def loop1_5():
	for i in range(1, 6):
		time.sleep(1)
		print(i)
 
def loop6_10():
	for i in range(6, 11):
		time.sleep(1)
		print(i)
 
def loop11_15():
	for i in range(11, 16):
		time.sleep(1)
		print(i)
 
def loop16_20():
	for i in range(16, 21):
		time.sleep(1)
		print(i)
 
def lightUp():
	if GPIO.input(22) :
		GPIO.output(22, GPIO.LOW)
		ledBtn["text"] = "LED\nOFF"
		ledBtn["bg"] = "#FF0000"
		statusBar["text"] = "LEDs éteintes"
	else:
		GPIO.output(22,GPIO.HIGH)
		ledBtn["text"] = "LED\nON"
		ledBtn["bg"] = "#70AD47"
		statusBar["text"] = "LEDs allumées"
 
def showInfo():
	tkMessageBox.showinfo("Informations", "Made by\nJorgio")
 
def exitProg():
	GPIO.cleanup()
	win.quit()
 
###########################
# PRÉPARATION DE LA FENÊTRE
win = Tk()
win.title("Interface d'administration")
win.geometry('480x500+300+100')
myFont = tkFont.Font(size=22, weight="bold")
 
# Découpage de la fenêtre
frameUp	= Frame(win)
frameDown = Frame(win)
frameUp.pack(side=TOP, padx=5, pady=5)
frameDown.pack(padx=5, pady=5)
 
# Construction des boutons
ledBtn	= Button(frameUp, text="LED\nOFF",	font=myFont, bg="#FF0000", command=dispatchCmd(1), height=4, width=7, padx=5, pady=5)
photoBtn	= Button(frameUp, text="PHOTO",	font=myFont, bg="#BBBBBB",command=dispatchCmd(2), height=4, width=7, padx=5, pady=5)
bellBtn	= Button(frameUp, text="BELL",		font=myFont, bg="#70AD47",command=dispatchCmd(3), height=4, width=7, padx=5, pady=5)
streamBtn	= Button(frameUp, text="STREAM",	font=myFont, bg="#70AD47",command=dispatchCmd(4), height=4, width=7, padx=5, pady=5)
zoomBtn	= Button(frameUp, text="ZOOM",	font=myFont, bg="#BBBBBB",command=dispatchCmd(5), height=4, width=7, padx=5, pady=5)
radarBtn	= Button(frameUp, text="RADAR", 	font=myFont, bg="#70AD47",command=dispatchCmd(6), height=4, width=7, padx=5, pady=5)
infoBtn	= Button(frameDown, text="INFO",	font=myFont, bg="#00A0FF", command=showInfo, height=4, width=7, padx=5, pady=5)
exitButton	= Button(frameDown, text="EXIT",	font=myFont, bg="#FF0000", command=exitProg,  height=4, width=7, padx=5, pady=5)
 
# Disposition des boutons
ledBtn.grid	(row=0, column=0)
photoBtn.grid	(row=0, column=1)
bellBtn.grid	(row=0, column=2)
streamBtn.grid	(row=1, column=0)
zoomBtn.grid	(row=1, column=1)
radarBtn.grid	(row=1, column=2)
infoBtn.grid	(row=2, column=1, padx=5)
exitBtn.grid	(row=2, column=2, padx=5)
 
# Barre d'état
statusBar = Label(win, text="Nothing to show...", bd=1, relief=SUNKEN, anchor=W)
statusBar.pack(side=BOTTOM, fill=X)
 
#####################
# PROGRAMME PRINCIPAL
if __name__ == '__main__':
	win.mainloop()
	win.destroy()