IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Python Discussion :

copier un objet (instance ?)


Sujet :

Python

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    52
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Septembre 2008
    Messages : 52
    Par défaut copier un objet (instance ?)
    Hello

    Je voudrai dupliquer a volonté un label ,mai en changer le texte:

    par exemple (code brouillon non fonctionnel):
    Avec un label la ligne des option devient vite très longue (bon on peux les couper m'en-fin c'est pas la question ici)
    je cherche a faire,(en très gros)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    def copielabel(texte)
       while True
          lab=Label(self, text =texte, justify=LEFT , anchor=SW , width =self.longStringLabel, height =1, relief = self.label3d ,background = self.labelBagroundColor ,foreground =self.labelForeground)
       return lab
     
     
    copielabel("texte 1")
    copielabel("texte 2")
    copielabel("texte 3")

    plutot que de faire:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    # Lfr[7] = liste contenan le text
        txt1 = Label(self, text =Lfr[7], justify=LEFT , anchor=SW , width =self.longStringLabel, height =1, relief = self.label3d ,background = self.labelBagroundColor ,foreground =self.labelForeground)
        txt2 = Label(self, text =Lfr[8], justify=LEFT , anchor=SW , width =self.longStringLabel, height =1, relief = self.label3d ,background = self.labelBagroundColor,foreground =self.labelForeground)
        txt3 = Label(self, text =Lfr[12], justify=LEFT , anchor=SW , width =self.longStringLabel, height =1, relief = self.label3d ,background = self.labelBagroundColor,foreground =self.labelForeground)
        txt4 = Label(self, text =Lfr[13], justify=LEFT , anchor=SW , width =self.longStringLabel, height =1, relief = self.label3d ,background = self.labelBagroundColor,foreground =self.labelForeground)
        txt5 = Label(self, text =Lfr[9], justify=LEFT , anchor=SW , width =self.longStringLabel, height =1, relief = self.label3d ,background = self.labelBagroundColor,foreground =self.labelForeground)
        txt6 = Label(self, text =Lfr[10], justify=LEFT , anchor=SW , width =self.longStringLabel, height =1, relief = self.label3d ,background = self.labelBagroundColor,foreground =self.labelForeground)
        txt7 = Label(self, text =Lfr[15], justify=LEFT , anchor=SW , width =self.longStringLabel, height =1, relief = self.label3d ,background = self.labelBagroundColor,foreground =self.labelForeground)
        txt8 = Label(self, text =Lfr[11], justify=LEFT , anchor=SW , width =self.longStringLabel, height =1, relief = self.label3d ,background = self.labelBagroundColor,foreground =self.labelForeground)
        txt9 = Label(self, text =Lfr[14], justify=LEFT , anchor=SW , width =self.longStringLabel, height =1, relief = self.label3d ,background = self.labelBagroundColor,foreground =self.labelForeground)
     
    #etc etc
    Merci d'avance

  2. #2
    Membre Expert Avatar de pacificator
    Profil pro
    Inscrit en
    Août 2006
    Messages
    1 074
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 1 074
    Par défaut
    Salut,

    Pourquoi ne pas utiliser une boucle for?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    self.labels = []
    for txt in ['texte1', 'texte2', 'texte3']:
        label= Label(self, text =txt, justify=LEFT , anchor=SW , width =self.longStringLabel, height =1, relief = self.label3d ,background = self.labelBagroundColor,foreground =self.labelForeground)
        self.labels.append(label)

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    52
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Septembre 2008
    Messages : 52
    Par défaut
    Merci pour ta réponse

    Et si je veux modfier 2 ou 3 élément par la suite ?
    modifer le text et justify=RIGHT ?

  4. #4
    Membre Expert Avatar de pacificator
    Profil pro
    Inscrit en
    Août 2006
    Messages
    1 074
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 1 074
    Par défaut
    Si tu peux modifier le comportement de tes widgets après leur affichage, tu peux faire quelque chose comme:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    self.labels[4].SetText('un texte')
    je n'utilise pas TKinter mais ce doit être possible, peut être, surement, faut voir....

  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    52
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Septembre 2008
    Messages : 52
    Par défaut
    oui mai sa reste toujours 1 paramètre à la fois ?

    si j'ai 30 control et que je doit modifier 3 paramètre par control sa me frai 90 ligne de code !

    il doit y avoir plus simple

  6. #6
    Membre Expert Avatar de pacificator
    Profil pro
    Inscrit en
    Août 2006
    Messages
    1 074
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 1 074
    Par défaut
    Si tu veux créer 30 labels differents, il faut bien que tu stockes leur configuration quelque part, non?

Discussions similaires

  1. [C#]Comment puis-je copier un objet ?
    Par lanuage dans le forum C#
    Réponses: 12
    Dernier message: 03/11/2006, 20h11
  2. [VB.NET] [VS 2003] Liste des objets instanciés
    Par Mouse dans le forum VB.NET
    Réponses: 4
    Dernier message: 23/10/2006, 19h15
  3. [debutant]Supprimer un objet instancié
    Par mikedavem dans le forum Général Java
    Réponses: 9
    Dernier message: 12/05/2006, 00h19
  4. Copier des objets d'un formulaire à un autre en VBA
    Par vincentdacol dans le forum Access
    Réponses: 1
    Dernier message: 24/04/2006, 14h18
  5. [FLASH MX2004] Valeurs d'objets instances de classes...
    Par amietbeach dans le forum Flash
    Réponses: 2
    Dernier message: 25/02/2006, 15h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo