Bonjour à tous.tes,

Je bloque depuis deux jours, sur le changement de texte d'un Label()

Ce petit bout de code fonctionne très bien chez moi :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
from tkinter import *
fen = Tk() 
def action():     
    labelBas.set("Je change le texte en cliquant") 
labelBas = StringVar() 
label2 = Label( fen, textvariable=labelBas) 
labelBas.set("taratata") 
b = Button(fen, text = "Test", command=action) 
b.pack() 
label2.pack() 
fen.mainloop()

Mais quand je veux l'utiliser dans mon exercice.

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
# Chargement des bibliothèques graphique et mathématique :
from tkinter import *
from math import *
 
# événements souris :
def pointeur(event):
    if choixAstre == 'deplTerre' :
        can.coords(terre, event.x-45,event.y-45, event.x+45,event.y+45)
    elif choixAstre == 'deplLune' :
        can.coords(lune, event.x-15,event.y-15, event.x+15,event.y+15)
    elif choixAstre == 'deplSatellite' :
        can.coords(satellite, event.x-7,event.y-7, event.x+7,event.y+7)
 
# événements boutons du menu :
def choixAstreT():
    global choixAstre
    choixAstre = 'deplTerre'
def choixAstreL():
    global choixAstre 
    choixAstre = 'deplLune'
def choixAstreS():
    global choixAstre 
    choixAstre = 'deplSatellite'
 
############################# Début du programme principal (main) ###############################
 
xl1, yl1 = 280, 150 # coordonnées initiales de la lune (vatiables globales)
xt1, yt1 = 100, 180 # coordonnées initiales de la terre (vatiables globales)
xs1, ys1 = 200, 250 # coordonnées initiales du satellite (vatiables globales)
choixAstre = ''
labelHaut = 'Label vide pour le moment'
labelBas = StringVar()
 
# calcul distance terre lune :
distTerreLune = sqrt(abs(xl1-yl1)**2 + abs(xt1-yt1)**2)
labelBas.set('Distance Terre Lune = ' + str(distTerreLune) + ' pixels')
 
# Création du widget principal ("maître") :
fen = Tk()
fen.title('Système solaire')                                            # texte haut de la fenètre
 
# création des widgets "esclaves" et leurs positionements :
label1 = Label(fen, text=labelHaut).grid(row =1, column =1, columnspan =2)
label2 = Label(fen, textvariable=labelBas).grid(row =22,  column =1, columnspan =2)
can = Canvas(fen,bg='black',height=400,width=600)
can.bind('<Button-1>', pointeur)                                        # souris dans le canva
terre = can.create_oval(xt1,yt1,xt1+90,yt1+90,width=2,fill='blue')      # widget terre
lune = can.create_oval(xl1,yl1,xl1+30,yl1+30,width=2,fill='white')      # widget lune   
satellite = can.create_oval(xs1,ys1,xs1+14,ys1+14,width=2,fill='red')   # widget satellite   
can.grid(row =2, column =1, rowspan =20)
# boutons du menu
Button(fen,text='Depl. Terre',width=15,command=choixAstreT).grid(row =2, column =2)
Button(fen,text='Depl. Lune',width=15,command=choixAstreL).grid(row =3, column =2)
Button(fen,text='Depl. Satellite',width=15,command=choixAstreS).grid(row =4, column =2)
Button(fen,text='Quitter',width=15,command=fen.quit).grid(row =21, column =2)
 
# démarrage du réceptionnaire d'évènements (boucle principale) :
fen.mainloop()
Je ne parviens même pas à initialiser ma variable "labelBas" avec StringVar().

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
PS C:\Users\rmanf301\OneDrive - Cafdoc\Studio\python> & C:/Users/rmanf/AppData/Local/Microsoft/WindowsApps/python3.11.exe "c:/Users/rmanf/OneDrive - Cafdoc/Studio/python/10G-astres-v4 copy.py"
Traceback (most recent call last):
  File "c:\Users\rmanf301\OneDrive - Cafdoc\Studio\python\10G-astres-v4 copy.py", line 32, in <module>
    labelBas = StringVar()
               ^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 562, in __init__
    Variable.__init__(self, master, value, name)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 393, in __init__
    master = _get_default_root('create variable')
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 319, in _get_default_root
    raise RuntimeError(f"Too early to {what}: no default root window")
RuntimeError: Too early to create variable: no default root window
PS C:\Users\rmanf3\OneDrive - Cafdoc\Studio\python>
Merci par avance pour votre aide.