Nom de variable généré automatiquement

Nom de variable généré automatiquement - Python - Programmation

Marsh Posté le 28-07-2004 à 17:10:08    

Voilà, j'ai une liste contenant des tuples.
Je souhaiterais instancier un bouton portant le nom du premier champ de mon objet

Reply

Marsh Posté le 28-07-2004 à 17:10:08   

Reply

Marsh Posté le 28-07-2004 à 17:17:50    

machin = (elem1, elem2, ..., elemn)
btn = wx.Button(parent, -1, machin[0]) (avec machin[0] un string, sinon faut le convertir en string d'abord)
 
crac t'as fini
c'est quand même pas difficile, si?
si le bouton existe déjà, tu fais btn.SetLabel(machin[0]) toujours en considérant que machin[0] est un string


---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 28-07-2004 à 17:27:36    

C'est justement le ton "btn" qui doit s'appeler machin[0] :jap:

Reply

Marsh Posté le 28-07-2004 à 17:55:27    

Essaie de regarder du côté de getattr/setattr, mais tu es obligé d'avoir un objet initial dont la variable à créer est membre.
 
En gros tu as un objet appelé truc, ben tu fais
setattr(truc, machin[0], x) avec machin[0] toujours un string et ca va creer un membre de truc avec comme nom le contenu de machin[0] et comme valeur 'x' (donc ici ca pourrait etre wx.Button(pleindetrucs)
getattr et delattr à regarder, tous membres des fonctions builtin (en gros fonctions directement accessibles dans Python sans faire d'import)


---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed