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 :

tkinter choix fichier dans liste a ouvrir


Sujet :

Python

Vue hybride

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

    Informations forums :
    Inscription : Décembre 2009
    Messages : 27
    Par défaut tkinter choix fichier dans liste a ouvrir
    Bonjour,
    je fais un programme avec tkinter qui permet d'ouvrir un fichier (avec import sys.)
    J'aimerai mettre un onglet qui permet de choisir le fichier dans un dossier, voila mon code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    liste_fichiers=os.listdir('dossier')
    ##cree une liste avec tous les fichiers du dossier
    for fichier in liste_fichiers:
    ##chaque fichier a son nom dans la barre de menu
        barre_menu.add_command(label=fichier, command=lambda  : changement_fichier(fichier))
    le menu est créé correctement, mais tous les onglets ont la même adresse, celle du dernier fichier de la liste. Comment pourrais-je faire pour que chaque onglet ait la bonne adresse?

  2. #2
    Membre Expert Avatar de PauseKawa
    Homme Profil pro
    Technicien Help Desk, maintenance, réseau, système et +
    Inscrit en
    Juin 2006
    Messages
    2 725
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Technicien Help Desk, maintenance, réseau, système et +
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2006
    Messages : 2 725
    Par défaut
    Bonjour,

    Vous devez donner fichier en argument à la fonction lambda.
    Un exemple simple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    import tkinter as tk
     
     
    root = tk.Tk()
    lab = tk.Label(root)
    lab.pack()
    for i in range(6):
        bout = tk.Button(root, text="Button %d" % i,
                         command=lambda val=i:
                             lab.config(text="%d selected" % val))
        bout.pack()
    root.mainloop()
    Vous trouverez bien des explications sur le forum avec une recherche sur 'lambda'.

    @+

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Décembre 2009
    Messages
    27
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Décembre 2009
    Messages : 27
    Par défaut
    ça marche, merci beaucoup

  4. #4
    Membre Expert Avatar de PauseKawa
    Homme Profil pro
    Technicien Help Desk, maintenance, réseau, système et +
    Inscrit en
    Juin 2006
    Messages
    2 725
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Technicien Help Desk, maintenance, réseau, système et +
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2006
    Messages : 2 725
    Par défaut
    Sans aucun doute.
    Mais pensez bien a faire une recherche sur lambda dans le sous forum Tkinter : Vous y trouverez bien plus.

    Bonne lecture et @+

    Edit : Un exemple.
    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
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    import tkinter
     
     
    def func(var):
        # print(var)
        display.config(text=str(var))
     
     
    win = tkinter.Tk()
    win.title("command & lambda")
     
    geomopts = {'padx':5, 'pady':5}
     
    codeframe = tkinter.Frame(win)
     
    xscrollbar = tkinter.Scrollbar(codeframe, orient=tkinter.HORIZONTAL)
    xscrollbar.pack(side=tkinter.BOTTOM, fill=tkinter.X)
     
    yscrollbar = tkinter.Scrollbar(codeframe)
    yscrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
     
    text = tkinter.Text(codeframe, wrap=tkinter.NONE,
                        xscrollcommand=xscrollbar.set,
                        yscrollcommand=yscrollbar.set)
    text.pack()
     
    xscrollbar.config(command=text.xview)
    yscrollbar.config(command=text.yview)
     
    code = """import tkinter
     
     
    def func(var):
        # print(var)
        display.config(text=str(var))
     
     
    win = tkinter.Tk()
    win.title("command & lambda")
     
    geomopts = {'padx':5, 'pady':5}
     
    codeframe = tkinter.Frame(win)
     
    xscrollbar = tkinter.Scrollbar(codeframe, orient=tkinter.HORIZONTAL)
    xscrollbar.pack(side=tkinter.BOTTOM, fill=tkinter.X)
     
    yscrollbar = tkinter.Scrollbar(codeframe)
    yscrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
     
    text = tkinter.Text(codeframe, wrap=tkinter.NONE,
                        xscrollcommand=xscrollbar.set,
                        yscrollcommand=yscrollbar.set)
    text.pack()
     
    xscrollbar.config(command=text.xview)
    yscrollbar.config(command=text.yview)
     
    widgetsframe = tkinter.Frame(win)
     
    display = tkinter.Label(widgetsframe, bd=1, relief=tkinter.SUNKEN, bg='white', fg='red')
    display.grid(row=0, column=0, columnspan=9,
                 sticky=tkinter.W+tkinter.E+tkinter.N+tkinter.S, **geomopts)
     
    tkinter.Label(widgetsframe, text="Whitout lambda",
                  fg='blue').grid(row=1, column=0, **geomopts)
    tkinter.Label(widgetsframe, text="lambda : func(value)",
                  fg='blue').grid(row=1, column=2, **geomopts)
    tkinter.Label(widgetsframe, text="lambda value=value: func(value)",
                  fg='blue').grid(row=1, column=4, **geomopts)
    tkinter.Label(widgetsframe, text="lambda value=staticentry.get(): func(value)",
                  fg='blue').grid(row=1, column=6, **geomopts)
    tkinter.Label(widgetsframe, text="lambda : func(variableentry.get())",
                  fg='blue').grid(row=1, column=8, **geomopts)
     
    for line in range(1, 6):
        tkinter.Button(widgetsframe, text="func %d" % (line),
                       command=func(line)).grid(row=line+2, column=0, **geomopts)
        tkinter.Button(widgetsframe, text="func %d" % (line),
                       command=lambda : func(line)).grid(row=line+2,
                                                         column=2,
                                                         **geomopts)
        tkinter.Button(widgetsframe, text="func %d" % (line),
                       command=lambda line=line: func(line)).grid(row=line+2,
                                                                  column=4,
                                                                  **geomopts)
     
    staticentry = tkinter.Entry(widgetsframe, bd=1, relief=tkinter.SUNKEN,
                                font=("Helvetica", "12", "bold italic"))
    staticentry.grid(row=4, column=6, **geomopts)
    staticentry.insert(0, "Enter text")
    staticentry.bind("<1>", lambda event: staticentry.delete(0, tkinter.END))
    variableentry = tkinter.Entry(widgetsframe, bd=1, relief=tkinter.SUNKEN,
                                  font=("Helvetica", "12", "bold italic"))
    tkinter.Button(widgetsframe, text="Apply text",
                   command=lambda value=staticentry.get(): func(value)).grid(row=5, column=6, **geomopts)
    variableentry.grid(row=4, column=8, **geomopts)
    variableentry.insert(0, "Enter text")
    variableentry.bind("<1>", lambda event: variableentry.delete(0, tkinter.END))
    tkinter.Button(widgetsframe, text="Apply text",
                   command=lambda : func(variableentry.get())).grid(row=5, column=8,
                                                                    **geomopts)
     
    tkinter.Frame(widgetsframe, height=2, bd=1, relief=tkinter.SUNKEN).grid(row=2, column=0, columnspan=9, sticky=tkinter.E+tkinter.W, **geomopts)
    for c in range(1, 9, 2):
        tkinter.Frame(widgetsframe, width=2, bd=1,
                      relief=tkinter.SUNKEN).grid(row=1, column=c, rowspan=7,
                                                  sticky=tkinter.N+tkinter.S,
                                                  **geomopts)
    tkinter.Frame(widgetsframe, height=2, bd=1, relief=tkinter.SUNKEN).grid(row=8, column=0, columnspan=9, sticky=tkinter.E+tkinter.W, **geomopts)
     
    codeframe.grid(row=0, column=0, columnspan=2, **geomopts)
    codeframe.grid_remove()
    widgetsframe.grid(row=0, column=0, columnspan=2, **geomopts)
     
    wb = tkinter.Button(win, text="Widgets",
                        command=lambda: codeframe.grid_remove() or widgetsframe.grid())
    wb.grid(row=1, column=0, **geomopts)
    wc = tkinter.Button(win, text="Code",
                        command=lambda: widgetsframe.grid_remove() or codeframe.grid())
    wc.grid(row=1, column=1, **geomopts)
    tkinter.Frame(win, height=2, bd=1, relief=tkinter.SUNKEN).grid(row=2, column=0, columnspan=9, sticky=tkinter.E+tkinter.W, **geomopts)
    wc = tkinter.Button(win, text="Exit", command=win.destroy)
    wc.grid(row=3, column=0, columnspan=2, **geomopts)
     
    win.mainloop()
    """
    text.insert(tkinter.END, code)
     
    widgetsframe = tkinter.Frame(win)
     
    display = tkinter.Label(widgetsframe, bd=1, relief=tkinter.SUNKEN, bg='white', fg='red')
    display.grid(row=0, column=0, columnspan=9,
                 sticky=tkinter.W+tkinter.E+tkinter.N+tkinter.S, **geomopts)
     
    tkinter.Label(widgetsframe, text="Whitout lambda",
                  fg='blue').grid(row=1, column=0, **geomopts)
    tkinter.Label(widgetsframe, text="lambda : func(value)",
                  fg='blue').grid(row=1, column=2, **geomopts)
    tkinter.Label(widgetsframe, text="lambda value=value: func(value)",
                  fg='blue').grid(row=1, column=4, **geomopts)
    tkinter.Label(widgetsframe, text="lambda value=staticentry.get(): func(value)",
                  fg='blue').grid(row=1, column=6, **geomopts)
    tkinter.Label(widgetsframe, text="lambda : func(variableentry.get())",
                  fg='blue').grid(row=1, column=8, **geomopts)
     
    for line in range(1, 6):
        tkinter.Button(widgetsframe, text="func %d" % (line),
                       command=func(line)).grid(row=line+2, column=0, **geomopts)
        tkinter.Button(widgetsframe, text="func %d" % (line),
                       command=lambda : func(line)).grid(row=line+2,
                                                         column=2,
                                                         **geomopts)
        tkinter.Button(widgetsframe, text="func %d" % (line),
                       command=lambda line=line: func(line)).grid(row=line+2,
                                                                  column=4,
                                                                  **geomopts)
     
    staticentry = tkinter.Entry(widgetsframe, bd=1, relief=tkinter.SUNKEN,
                                font=("Helvetica", "12", "bold italic"))
    staticentry.grid(row=4, column=6, **geomopts)
    staticentry.insert(0, "Enter text")
    staticentry.bind("<1>", lambda event: staticentry.delete(0, tkinter.END))
    variableentry = tkinter.Entry(widgetsframe, bd=1, relief=tkinter.SUNKEN,
                                  font=("Helvetica", "12", "bold italic"))
    tkinter.Button(widgetsframe, text="Apply text",
                   command=lambda value=staticentry.get(): \
                   func(value)).grid(row=5, column=6, **geomopts)
    variableentry.grid(row=4, column=8, **geomopts)
    variableentry.insert(0, "Enter text")
    variableentry.bind("<1>", lambda event: variableentry.delete(0, tkinter.END))
    tkinter.Button(widgetsframe, text="Apply text",
                   command=lambda : func(variableentry.get())).grid(row=5, column=8,
                                                                    **geomopts)
     
    tkinter.Frame(widgetsframe, height=2, bd=1, relief=tkinter.SUNKEN).grid(row=2, \
        column=0, columnspan=9, sticky=tkinter.E+tkinter.W, **geomopts)
    for c in range(1, 9, 2):
        tkinter.Frame(widgetsframe, width=2, bd=1,
                      relief=tkinter.SUNKEN).grid(row=1, column=c, rowspan=7,
                                                  sticky=tkinter.N+tkinter.S,
                                                  **geomopts)
    tkinter.Frame(widgetsframe, height=2, bd=1, relief=tkinter.SUNKEN).grid(row=8, \
        column=0, columnspan=9, sticky=tkinter.E+tkinter.W, **geomopts)
     
    codeframe.grid(row=0, column=0, columnspan=2, **geomopts)
    codeframe.grid_remove()
    widgetsframe.grid(row=0, column=0, columnspan=2, **geomopts)
     
    wb = tkinter.Button(win, text="Widgets",
                        command=lambda: codeframe.grid_remove() or \
                                        widgetsframe.grid())
    wb.grid(row=1, column=0, **geomopts)
    wc = tkinter.Button(win, text="Code",
                        command=lambda: widgetsframe.grid_remove() or \
                                        codeframe.grid())
    wc.grid(row=1, column=1, **geomopts)
    tkinter.Frame(win, height=2, bd=1, relief=tkinter.SUNKEN).grid(row=2, \
        column=0, columnspan=9, sticky=tkinter.E+tkinter.W, **geomopts)
    wc = tkinter.Button(win, text="Exit", command=win.destroy)
    wc.grid(row=3, column=0, columnspan=2, **geomopts)
     
    win.mainloop()

  5. #5
    Membre Expert
    Profil pro
    Développeur en systèmes embarqués retraité
    Inscrit en
    Mars 2006
    Messages
    952
    Détails du profil
    Informations personnelles :
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur en systèmes embarqués retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2006
    Messages : 952
    Par défaut
    Salut,

    Citation Envoyé par PauseKawa Voir le message
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    code = """import tkinter .../...
    Et comme ça?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    with open(sys.argv[0], "r") as fp:
        code = fp.read(-1)
    Je préfère, car on est ainsi sûr que si on modifie le source sa copie sera identique.

    A+

    Pfeuh

  6. #6
    Membre Expert Avatar de PauseKawa
    Homme Profil pro
    Technicien Help Desk, maintenance, réseau, système et +
    Inscrit en
    Juin 2006
    Messages
    2 725
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Technicien Help Desk, maintenance, réseau, système et +
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2006
    Messages : 2 725
    Par défaut
    C'était juste pour ne pas en rajouter trop
    Maintenant si le po intègre l'information pourquoi pas.

    @+

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 3
    Dernier message: 06/05/2015, 10h31
  2. Import d'un fichier dans liste contacts
    Par yanis97 dans le forum Développement Sharepoint
    Réponses: 1
    Dernier message: 18/02/2013, 16h43
  3. Choix de la liste déroulante qui permet d'ouvrir un autre fichier
    Par carocaro630 dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 25/01/2013, 09h18
  4. Réponses: 0
    Dernier message: 30/06/2010, 16h27
  5. Réponses: 4
    Dernier message: 24/04/2003, 22h28

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