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

  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?

  7. #7
    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
    donc voila un code fonctionnel ou presque la ligne
    #self.labels[0].SetText('un texte')
    ne marche pas ?
    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
     
    #!/usr/bin/env python
    # -*- coding:Utf-8 -*-
    import ConfigParser
    from Tkinter import *
    #paquet python-pil
    from PIL import Image,ImageTk
    import os
    import shutil
     
    class Win:
        def __init__(self,largeur=800,hauteur=600):
            self.CanL=largeur
            self.CanH=hauteur
            self.H_image=largeur/2
            self.L_image=hauteur/2
            self.longStringLabel=40 # longueur dans un label (string)
            self.label3d= GROOVE #RAISED SUNKEN, GROOVE, and RIDGE.
            self.labelBagroundColor="white" #"red", "yellow", "blue", "green", "black"
            self.labelForeground="blue"
        def chargement(self):
            self.tk =Tk()
            self.tk.title('build kernel')
            my_file="/home/taz/Desktop/python/cadre.png"
            can = Canvas(self.tk, width =self.CanL, height =self.CanH, bg ='white')
            mon_image=ImageTk.PhotoImage(file=my_file)
            can.create_image(self.H_image,self.L_image,image=mon_image)
            self.labels = []
            label=Label(self.tk, text ="", justify=LEFT , anchor=SW , width =self.longStringLabel, height =1, relief = self.label3d ,background = self.labelBagroundColor ,foreground =self.labelForeground).grid(row =5, column =10)
            self.labels.append(label)
            #self.labels[0].SetText('un texte')
     
            can.grid(row = 0, rowspan = 20,column =2, columnspan=20, sticky=N)
            self.tk.mainloop()
     
    start = Win()
    start.chargement()
    dans le init je peux donc avoir les valeur par défaut, mai ensuite j'accède comment aux propriété des différent paramètre ?

    Merci pour ta réponse

  8. #8
    Expert confirmé
    Avatar de fred1599
    Homme Profil pro
    Lead Dev Python
    Inscrit en
    Juillet 2006
    Messages
    4 062
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Lead Dev Python
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Juillet 2006
    Messages : 4 062
    Par défaut
    et pourquoi ne pas utiliser %s

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    lab=Label(self, text =%s, justify=%s , anchor=SW , width =self.longStringLabel, height =1, relief = self.label3d ,background = self.labelBagroundColor ,foreground =self.labelForeground) %(texte, justifier)

  9. #9
    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
    Citation Envoyé par fred1599 Voir le message
    et pourquoi ne pas utiliser %s

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    lab=Label(self, text =%s, justify=%s , anchor=SW , width =self.longStringLabel, height =1, relief = self.label3d ,background = self.labelBagroundColor ,foreground =self.labelForeground) %(texte, justifier)
    euh % ser a concaténer les objet (un patron en quelque sorte) mai j'en voit pas l'utilité.
    Je veu pas les concaténer, mai les crée pour pouvoir ensuite les modifier avec un minimum de ligne sans compter les autre control genre button et compagnie ?

    Merci pour les réponses

  10. #10
    Expert confirmé
    Avatar de fred1599
    Homme Profil pro
    Lead Dev Python
    Inscrit en
    Juillet 2006
    Messages
    4 062
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Lead Dev Python
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Juillet 2006
    Messages : 4 062
    Par défaut
    Et le module copy ne ferait pas ton affaire?

    http://docs.python.org/lib/module-copy.html

  11. #11
    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
    Que veux-tu faire exactement?

    - Generer 30 label identiques puis modifier leurs propriétés
    - Generer 30 label differents?

  12. #12
    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
    Citation Envoyé par pacificator Voir le message
    Que veux-tu faire exactement?

    - Generer 30 label identiques puis modifier leurs propriétés
    - Generer 30 label differents?
    - Generer 30 label identiques puis modifier leurs propriétés <<--- celui là

    Merci pour les réponse

  13. #13
    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
    bon finalement. je me suis contenter de faire une boucle

    Merci a tous

  14. #14
    Membre émérite
    Homme Profil pro
    heu...
    Inscrit en
    Octobre 2007
    Messages
    648
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : heu...

    Informations forums :
    Inscription : Octobre 2007
    Messages : 648
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    self.labels = []
    label=Label(self.tk, text ="", justify=LEFT , anchor=SW , width =self.longStringLabel, height =1, relief = self.label3d ,background = self.labelBagroundColor ,foreground =self.labelForeground).grid(row =5, column =10)
    self.labels.append(label)
    Tu pourrais directement mettre :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    self.labels = []
    self.labels.append(Label(self.tk, text ="", justify=LEFT , anchor=SW , width =self.longStringLabel, height =1, relief = self.label3d ,background = self.labelBagroundColor ,foreground =self.labelForeground).grid(row =5, column =10))
    Après pour modifier le texte d'un label, tu peux faire quelquechose comme :
    self.labels[6]['text']='Wahou, le texte d\'enfer!'

  15. #15
    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, voila un peux ce que sa donne: je débute donc
    je colle pas le code en entier car cela deviendrai hors sujet.



    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
     
            self.tk =Tk()
            self.tk.title('build kernel')
            listLab = []
            listEntry = []
            I=0
     
            while I < self.NumberLabel:
                listLab.append((Label(self.tk, text =FrList[I] , \
                justify=self.Ljustify ,\
                anchor=self.Lanchor,\
                width = self.longStringLabel,\
                height = self.Lheight,\
                relief = self.label3d,\
                background = self.labelBagroundColor,\
                foreground = self.labelForeground)))
                #listLab[I].configure(text="sdffffffffffffffffd")
                #listLab[I].configure(background = "red")
                listLab[I].grid(row = I + self.TopPosLabel, column = self.ColLabel)
                Var = StringVar()
                listEntry.append(Entry(self.tk,width =self.entryLenCar ,background = self.labelBagroundColor, text = Var))
                listEntry[I].grid(row = I + self.TopPosEntry, column= self.ColEntry)
                Var.set(ListConf[I])
                I = I +1
    self.tk.mainloop()
    voila :

    maintenant j'ai une autres question comment connaître l'évènement concernant les Entry. Pour vérifier ce qui est en court de frappe ?
    si quelqu'un a un lien sur la syntaxe pour écrire l'événement je suis preneur

    Merci pour les réponse

  16. #16
    Membre émérite
    Homme Profil pro
    heu...
    Inscrit en
    Octobre 2007
    Messages
    648
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : heu...

    Informations forums :
    Inscription : Octobre 2007
    Messages : 648
    Par défaut
    Tiens, voici un document pdf qui couvre une très grosse partie de Tkinter, dont les évenements:
    http://www.nmt.edu/tcc/help/lang/python/tkinter.pdf
    Je crois bien qu'il y a un évènement qui correspond parfaitement à ce que tu veux, au pire faudra faire plein de binds via un boucle for et la fonction exec (built-in function of python)

  17. #17
    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 , malgré que sa soit en anglais je pense que sa va me servir :

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