Bonjour,

Une liste déroulante qui me permet de choisir une nation, ça marche, je récupère le nom de ma nation dans la variable q :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
q = lstDeroulNation.get()
print(Allemagne.capitale)
print(q)
print(q.capitale)

Cela me donne :
Berlin
Allemagne
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\jm.aix\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "C:/0_Mes_codes/Civ_Lancement.py", line 27, in foncChoixNation
print(q.capitale)
AttributeError: 'str' object has no attribute 'capitale'



q renvoi bien "Allemagne" (string)
Allemagne.capitale renvoi bien "Berlin"
q.capitale renvoi une erreur car q est un string et il le faut en objet
si je fait :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
q=Allemagne
print(q.capitale)
Là ça marche

Comment fait-on pour que q récupère en tant qu'objet ?

Code complet :

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
# Imports
from tkinter import *
import tkinter.ttk as ttk
 
# ____________________________________________________________________________________________________________________
# Les variables
varCoulPrinc = "Slate Gray"
varCoulPrinc2 = "Gray"
# ____________________________________________________________________________________________________________________
# ____________________________________________________________________________________________________________________
# Création de l'instance Tk
fen0 =Tk()
# ____________________________________________________________________________________________________________________
# Les fonctions
def foncChoixNation(event):
# Suite choix déroulante liste des nations : affichage dans le label
q = lstDeroulNation.get()
lab00['text'] = "Vous avez choisi la nation : " + q
 
 
print(Allemagne.capitale)
print(q)
q=Allemagne
print(q.capitale)
 
 
 
 
 
# ____________________________________________________________________________________________________________________
# Les classes
class nation:
nbNation = 0
lstNation = []
 
def __init__(self, nom, couleur, dirigeant, capitale):
global lstNation
nation.nbNation += 1
self.nom = nom
self.couleur = couleur
self.dirigeant = dirigeant
self.capitale = capitale
# Ajout à la liste des nations pour la liste déroulante
nation.lstNation.append(nom)
 
 
 
# ____________________________________________________________________________________________________________________
# ____________________________________________________________________________________________________________________
# MAIN : Lancement du programme Principal
# ____________________________________________________________________________________________________________________
# Création des nations
Allemagne = nation("Allemagne", "gris", "Otto Von Bismarck", "Berlin")
Angleterre = nation("Angleterre", "rouge", "Elisabeth 1ère", "Londres")
France = nation("France", "bleu", "Napoléon 1er", "Paris")
nbNation = int(nation.nbNation)
# _______________________________________
# TKinter
# _______________________________________
# Création fenêtre principale
fen0.geometry('500x300')
fen0.title("Civ_Fenêtre principale")
fen0.configure(bg = varCoulPrinc)
# _______________________________________
# listes déroulantes
lstDeroulNation = ttk.Combobox(fen0, width=16, values=(nation.lstNation))
lstDeroulNation.bind('<<ComboboxSelected>>', foncChoixNation)
# Création labels
lab00 = Label(fen0, text = "",width=50, bg=varCoulPrinc2)
 
 
 
 
# _______________________________________
# positionnement objets
lstDeroulNation.grid(row=0, columns =1) # Liste déroulante Nation
lab00.grid(row=1, columns =2)
# Centrage fenêtre
from Fonctions.FoncCentreEcran import centreFenetre
centreFenetre(fen0)
# _______________________________________
# Réception d'événements
fen0.mainloop()