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 :

Retour tkinter utf8 b'


Sujet :

Python

  1. #1
    Modérateur

    Avatar de MaitrePylos
    Homme Profil pro
    DBA
    Inscrit en
    Juin 2005
    Messages
    5 496
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : Belgique

    Informations professionnelles :
    Activité : DBA
    Secteur : Service public

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 496
    Points : 12 596
    Points
    12 596
    Par défaut Retour tkinter utf8 b'
    Bonjour,
    Je modifie un script qui me permet de générer mes passwd, en son temps il à été écrit en Python2, donc je en fait que l'adapter, je suis bon sauf que quand je fais un copier depuis tkinter, il me retourne un string préfixé de b'
    Je n'arrive pas à faire un simple copié de ma str en utf8 sans ce b'

    cela se passe ici :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
      def copy_result(self, *args):
            """Place the site-specific password on the clipboard."""
            if self.result.get():
                self.result_entry.focus()
                self.result_entry.selection_range(0, 'end')
                self.parent.clipboard_clear()
                self.parent.clipboard_append(self.result.get())

    Voici le 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
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
     
    # coding: utf8
    # Copyright 2002, Hewlett-Packard
    #
    # A little program to hash a user password with a site-specific string
    # The result is a random looking string that can be used as a password
    # on that site.  This tool lets you enter the same password string for
    # all sites, but gives you a unique password to use for each site.
    #
    # 2002-02-07 - Initial VERSION 0.1
    # 2002-02-07 - Changed result field so it can be cut and pasted, VERSION 0.2
    # 2005-04-24 - Added alphanumeric option and clipboard handling, VERSION 0.3
    # 2015-11-05 - Modify for Python 3 - Add encode('utf-8') and add import hashlib, VERSION 0.4
    #
    # To do:
    # 1. Make a browser plugin that pops up when a password field appears
    # 2. Port to Jornada
    #
    # Alan Karp, alan.karp@hp.com, 7 February 2002
    # Ka-Ping Yee, zestyping@hp.com, 24 April 2005
    # Ernaelsten Gérard info@formatux.be 04 November 2015
    import base64
    import hashlib
     
    from tkinter import *
     
    VERSION = '0.4'
    LABEL_FONT = ('Helvetica', 9)
    ENTRY_FONT = ('Courier', 10)
     
     
    def show_help():
        """Display the help window."""
        window = Toplevel()
        Message(window, text="""
                                    Site-Specific Password Generator
                                    (Patent Pending, Hewlett-Packard)
     
    This tool combines your password with a name for a website to produce a
    password unique to the site.  Using this tool, you only have to memorize
    one password for all your Web logins, without worrying that anyone who
    knows your password for one site will be able to use it on another one.
     
    There are two input fields:
     
            Your password
                    A string that you can remember.  It should still be hard for others
                    to guess because this tool uses a well-known algorithm and you
                    use a well-known site name.  Case sensitive.
            Site name
                    Your name for the site.  For example, Schwab or Amazon.
                    Case insensitive.
     
    The site-specific password appears in the third field.  It will be 12 ASCII
    characters.  Click "Copy to Clipboard" or just press the Enter key to put the
    generated password on the clipboard so you can paste it into the password
    field on the web site.  If you are using a site that accepts only numbers and
    letters in passwords, check "Letters and numbers only".
            """, font=LABEL_FONT).pack()
        Button(window, text='Close', font=LABEL_FONT, command=window.destroy).pack()
     
     
    def generate_password(password, sitename, alphanumeric=0):
        """Compute a site-specific password."""
        # Use the MD5 hash algorithm to combine the inputs.
        sitename = sitename.encode('utf-8')
        password = password.encode('utf-8')
        digest = hashlib.md5(sitename.lower() + password).digest()
        # Convert the hash to ASCII characters.
        digest = base64.encodestring(digest)
        intermediaire = digest.replace('+'.encode('utf-8'), 'A'.encode('utf-8'))
        digest = intermediaire
        intermediaire = digest.replace('/'.encode('utf-8'), 'B'.encode('utf-8'))
        digest = intermediaire
     
        if alphanumeric:
            digest = '/'.encode('utf-8') + digest[1:]
     
        return digest[:12]
     
     
    class SitePasswordForm:
        def __init__(self, parent):
            self.parent = parent
     
            # variables
            self.password = StringVar(parent)
            self.sitename = StringVar(parent)
            self.result = StringVar(parent)
            self.alpha = BooleanVar(parent)
            self.password.trace('w', self.update_password)
            self.sitename.trace('w', self.update_password)
            self.alpha.trace('w', self.update_password)
     
            # password entry field
            self.password_label = Label(parent, text='Your password:', font=LABEL_FONT)
            self.password_entry = Entry(parent, show='*', font=ENTRY_FONT,
                                        textvar=self.password)
     
            # site entry field
            self.sitename_label = Label(parent, text='Site name:', font=LABEL_FONT)
            self.sitename_entry = Entry(parent, font=ENTRY_FONT, textvar=self.sitename)
     
            # output field
            self.result_label = Label(parent, text='Site password:', font=LABEL_FONT)
            self.result_entry = Entry(parent, state='readonly', relief=RIDGE,
                                      font=ENTRY_FONT, textvar=self.result)
     
            # check box
            self.alpha_check = Checkbutton(parent, text='Include punctuation',
                                           font=LABEL_FONT, variable=self.alpha)
     
            # buttons
            self.button_frame = Frame(parent)
            self.copy_button = Button(self.button_frame, text='Copy to Clipboard (Enter)',
                                      font=LABEL_FONT, command=self.copy_result, padx=10)
            self.help_button = Button(self.button_frame, text='Help',
                                      font=LABEL_FONT, command=show_help, padx=10)
     
            # layout
            self.password_label.grid(row=0, column=0, sticky=E)
            self.password_entry.grid(row=0, column=1)
            self.sitename_label.grid(row=1, column=0, sticky=E)
            self.sitename_entry.grid(row=1, column=1)
            self.result_label.grid(row=2, column=0, sticky=E)
            self.result_entry.grid(row=2, column=1)
            self.alpha_check.grid(row=3, column=1, sticky=W)
            self.copy_button.pack(side=LEFT)
            self.help_button.pack(side=LEFT)
            self.button_frame.grid(row=4, column=0, columns=2)
     
            # key bindings
            self.password_entry.bind('<Return>', self.handle_return)
            self.sitename_entry.bind('<Return>', self.handle_return)
     
            # startup
            self.update_password()
            self.password_entry.focus()
     
        def update_password(self, *args):
            """Update the site-specific password field."""
            password = self.password.get()
            password.strip()
            sitename = self.sitename.get()
            sitename.strip()
            alpha = self.alpha.get()
            if password and sitename:
                self.result.set(generate_password(password, sitename, alpha))
                self.copy_button['state'] = ACTIVE
            else:
                self.result.set('')
                self.copy_button['state'] = DISABLED
     
        def handle_return(self, *args):
            """When the user presses Enter, advance to the next obvious step."""
            password = self.password.get()
            sitename = self.sitename.get()
            if not password.strip():
                self.password_entry.focus()
            elif not sitename.strip():
                self.sitename_entry.focus()
            else:
                self.copy_result()
     
        def copy_result(self, *args):
            """Place the site-specific password on the clipboard."""
            if self.result.get():
                self.result_entry.focus()
                self.result_entry.selection_range(0, 'end')
                self.parent.clipboard_clear()
                self.parent.clipboard_append(self.result.get())
     
     
    root = Tk()
    root.title('Site-Specific Password ' + VERSION)
    SitePasswordForm(root)
    root.mainloop()

    Merci de votre aide.

  2. #2
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 287
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 287
    Points : 36 776
    Points
    36 776
    Par défaut
    Salut,

    Pouvez vous expliciter comment reproduire le problème constaté?
    Après un essai, la méthode:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
        def copy_result(self, *args):
            """Place the site-specific password on the clipboard."""
            if self.result.get():
                print(type(self.result.get()))
                self.result_entry.selection_range(0, 'end')
                self.parent.clipboard_clear()
                self.parent.clipboard_append(self.result.get())
    récupère bien un "str" via self.result.get() sans "b" devant.

    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  3. #3
    Modérateur

    Avatar de MaitrePylos
    Homme Profil pro
    DBA
    Inscrit en
    Juin 2005
    Messages
    5 496
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : Belgique

    Informations professionnelles :
    Activité : DBA
    Secteur : Service public

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 496
    Points : 12 596
    Points
    12 596
    Par défaut
    Visuellement oui c'est bon,

    Par contre si je pousse sur le bouton 'Copy to cliboard' et à cette condition seulement, le copié/collé donne un truc du genre

    Dans ce cas j'ai bien b'texte'

  4. #4
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 287
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 287
    Points : 36 776
    Points
    36 776
    Par défaut
    Citation Envoyé par MaitrePylos Voir le message
    Par contre si je pousse sur le bouton 'Copy to cliboard' et à cette condition seulement, le copié/collé donne un truc du genre

    Dans ce cas j'ai bien b'texte'
    "le copié/collé donne un truc du genre" ? L'appel à copy_result "pousse" des choses dans le clipboard. Si je paste çà "ailleurs", je n'ai pas de b'...'
    Vous travaillez avec quel OS/version de Python?

    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  5. #5
    Modérateur

    Avatar de MaitrePylos
    Homme Profil pro
    DBA
    Inscrit en
    Juin 2005
    Messages
    5 496
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : Belgique

    Informations professionnelles :
    Activité : DBA
    Secteur : Service public

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 496
    Points : 12 596
    Points
    12 596
    Par défaut
    Python3.4/Fedora23

  6. #6
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 287
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 287
    Points : 36 776
    Points
    36 776
    Par défaut
    Salut,

    Je n'ai pas ce genre de gadget en magasin.
    Essayez avec:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
              self.parent.clipboard_append(self.result.get().encode('utf-8'))
    Si çà ne fonctionne pas (ce qui est fort probable), il faudra attendre que quelqu'un ait reproduit le soucis sur un environnement semblable.

    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  7. #7
    Modérateur

    Avatar de MaitrePylos
    Homme Profil pro
    DBA
    Inscrit en
    Juin 2005
    Messages
    5 496
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : Belgique

    Informations professionnelles :
    Activité : DBA
    Secteur : Service public

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 496
    Points : 12 596
    Points
    12 596
    Par défaut
    J'avais déjà essayé, mais cela ne resoud pas mon souci.
    Enfin.

    Merci de t'être penché sur mon problème.

    MaitrePylos

Discussions similaires

  1. Réponses: 8
    Dernier message: 12/02/2013, 01h08
  2. Retour au mode texte par défaut
    Par coca dans le forum x86 16-bits
    Réponses: 6
    Dernier message: 12/12/2002, 17h22
  3. [XP] Retour d'experience
    Par virgile04 dans le forum Méthodes Agiles
    Réponses: 10
    Dernier message: 22/10/2002, 08h25
  4. Retour chariot dans un TMemo ?
    Par Vincent PETIT dans le forum C++Builder
    Réponses: 7
    Dernier message: 27/08/2002, 18h55
  5. URGENT: retour de string
    Par Pinggui dans le forum CORBA
    Réponses: 4
    Dernier message: 15/07/2002, 09h47

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