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

Tkinter Python Discussion :

Erreur sur effacement contenu Listbox


Sujet :

Tkinter Python

  1. #1
    Membre régulier
    Homme Profil pro
    Touche à tout
    Inscrit en
    Mai 2017
    Messages
    441
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : Touche à tout

    Informations forums :
    Inscription : Mai 2017
    Messages : 441
    Points : 111
    Points
    111
    Par défaut Erreur sur effacement contenu Listbox
    Bonjour,

    J'alimente une Listbox selon le choix d'une ComboBox. Lorsque je change ma sélection dans ma ComboBox, ma ListBox s'efface bien et affiche le nouveau contenu.

    Par contre lorsuqe je sélectionne une valeur dans ma ListBox et qu'après je fais une nouvelle sélection dans ma ComboBox j'ai le message d'erreur suivant:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\Python_3.7\lib\tkinter\__init__.py", line 1702, in __call__
        return self.func(*args)
      File "U:\_Developpement\PYTHON\toto.py", line 121, in <lambda>
        Cell_listbox.bind('<<ListboxSelect>>', lambda e: FTP_GET_filesinfolder())
      File "U:\_Developpement\PYTHON\toto.py", line 69, in FTP_GET_filesinfolder
        CELL_folder = Cell_listbox.get(Cell_listbox.curselection())
      File "C:\Python_3.7\lib\tkinter\__init__.py", line 2795, in get
        return self.tk.call(self._w, 'get', first)
    _tkinter.TclError: bad listbox index "": must be active, anchor, end, @x,y, or a number
    Merci par avance. Ci dessous mon code:

    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
     
    from configparser import ConfigParser
    from tkinter import *
    from tkinter import ttk
    from tkinter.ttk import *
    from tkinter import font
    from tkinter import Listbox
    import ftplib
    import os
     
    #Application parameters
    #FTP parametrers
    CFG_FILE             = ConfigParser()
    CFG_FILE.optionxform = str
    CFG_FILE.read('ini/application.ini')
    print(os.getlogin()+' connected')
     
    APPLICATION_NAME = CFG_FILE.get('APPLICATION_PARAMETERS', 'APPLICATION_NAME')
    global FTP_USER, FTP_PASSWORD
    FTP_USER         = CFG_FILE.get('SECRET', 'USER')
    FTP_PASSWORD     = CFG_FILE.get('SECRET', 'PASSWORD')
    #END FTP
     
    LINE_NAME        = CFG_FILE.options('NETWORK_PARAMETERS')
    NUMBER_OF_LINE   = len(LINE_NAME)
     
    #Functions
    def FTP_GET_folder():
        global SELECTED_LINE
        print(ComboBox_linechoice.get()+' selected')
        SELECTED_LINE=ComboBox_linechoice.get()
        #Cell_listbox.delete(0, END)
     
        #ftp connexion
        FTP_PARAMETERS              = CFG_FILE.get('NETWORK_PARAMETERS', SELECTED_LINE)
        SERVER_NAME, PORT, FTP_PATH = FTP_PARAMETERS.split(";")
     
        ftp = ftplib.FTP()
        ftp.connect(SERVER_NAME, int(PORT))
        ftp.login(FTP_USER, FTP_PASSWORD)
     
        Names      = ftp.nlst(FTP_PATH)
        CurrentDir = ftp.pwd()
        SubDirs    = []
     
        for name in Names:
            chemin = name    
            okdir  = GET_Only_directories(ftp, chemin)
            if okdir:
                SubDirs.append(name)
     
        Cell_listbox.delete(0, END)
     
        for SubDir in SubDirs:
            SubDirSplit      = SubDir.split("/")
            LenOfSubDirSplit = len(SubDirSplit)      
            Cell_listbox.insert(END, SubDirSplit[LenOfSubDirSplit-1])
     
        ftp.close()
     
    def FTP_GET_filesinfolder():
        CELL_folder = Cell_listbox.get(Cell_listbox.curselection())
     
        #ftp connexion
        FTP_PARAMETERS              = CFG_FILE.get('NETWORK_PARAMETERS', SELECTED_LINE)
        SERVER_NAME, PORT, FTP_PATH = FTP_PARAMETERS.split(";")
        ftp = ftplib.FTP()
        ftp.connect(SERVER_NAME, int(PORT))
        ftp.login(FTP_USER, FTP_PASSWORD)   
        ftp.close()
     
    def FTP_READ_file():
        print('FTP_READ_file')
     
    def GET_Only_directories(ftp, chemin):
        CurrentDir = ftp.pwd()
        try:
            ftp.cwd(chemin)
            ftp.cwd(CurrentDir)
            return True
        except Exception:
            return False
     
    #End of functions
     
    #################################################################################################
    #################################################################################################
     
    #MainGUI
    MainGUI = Tk()
    MainGUI.title(APPLICATION_NAME)
    MainGUI.iconbitmap(r'ini/graph_icon.ico')
    MainGUI.geometry('1200x600')
    Mainfont = font.Font(MainGUI, family='Courier new', size='10')
     
    #LabelFrame for curve selection
    labelframe = LabelFrame(MainGUI, text="SELECTION",  width='20')
    labelframe.grid(padx=5, pady=5)
     
    #Label for ComboBox line choice
    Label_linechoice = Label(MainGUI, text="Select a line:", font=Mainfont).grid(row=0, padx=5, pady=5, sticky=W)
     
    #ComboBox line selection
    ComboBox_linechoice = ttk.Combobox(values=LINE_NAME, state="readonly", font=Mainfont)
     
    #ComboBox_linechoice.set('Select a line')
    ComboBox_linechoice.bind('<<ComboboxSelected>>', lambda e: FTP_GET_folder()) #print(ComboBox_linechoice.get()))
    ComboBox_linechoice.grid(row=1, padx=5)
     
    #Label for ListView cell choice
    Label_cellchoice = Label(MainGUI, text="Select a cell:", font=Mainfont).grid(row=2, padx=5, pady=5, sticky=W)
     
    Cell_listbox = Listbox(MainGUI, font=Mainfont, height=16, width=20)
    Cell_listbox.bind('<<ListboxSelect>>', lambda e: FTP_GET_filesinfolder())
    Cell_listbox.grid(padx=5, column=0, sticky='w')
     
    MainGUI.mainloop()

  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 778
    Points
    36 778
    Par défaut
    Salut,

    Le message d'erreur vous dit juste que ce que retourne listbox.curselection (un tuple) ne peut pas est utilisé directement comme paramètre à listbox.get.
    Relire la documentation, coder un petit exemple d'utilisation pour voir comment faire fonctionner tout çà (avant de l'utiliser dans un code plus important),... vous permettrait de poster un code bien plus réduit en cas de soucis.

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

  3. #3
    Membre régulier
    Homme Profil pro
    Touche à tout
    Inscrit en
    Mai 2017
    Messages
    441
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : Touche à tout

    Informations forums :
    Inscription : Mai 2017
    Messages : 441
    Points : 111
    Points
    111
    Par défaut
    Du coup, j'ai fais ça et plus d'erreur dans la console

    Par contre, je souhaite rendre 'disabled' ma ListBox pendant la recherche de fichiers, mais ça ne fonctionne pas comme je le voudrais...

    Je ne sais pas où mettre le code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Cell_listbox.configure(state=DISABLED)
    et
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Cell_listbox.configure(state=NORMAL)
    ...

    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
    from configparser import ConfigParser
    from tkinter import *
    from tkinter import ttk
    from tkinter.ttk import *
    from tkinter import font
    from tkinter import Listbox
    import ftplib
    import os
     
    #Application parameters
    #FTP parametrers
    CFG_FILE             = ConfigParser()
    CFG_FILE.optionxform = str
    CFG_FILE.read('ini/application.ini')
    print(os.getlogin()+' connected')
     
    APPLICATION_NAME = CFG_FILE.get('APPLICATION_PARAMETERS', 'APPLICATION_NAME')
    global FTP_USER, FTP_PASSWORD
    FTP_USER         = CFG_FILE.get('SECRET', 'USER')
    FTP_PASSWORD     = CFG_FILE.get('SECRET', 'PASSWORD')
    #END FTP
     
    LINE_NAME        = CFG_FILE.options('NETWORK_PARAMETERS')
    NUMBER_OF_LINE   = len(LINE_NAME)
     
    #global hidden_ext = 
     
    #Functions
    def FTP_GET_folder():
        global SELECTED_LINE
        print(ComboBox_linechoice.get()+' selected')
        SELECTED_LINE=ComboBox_linechoice.get()
        #Cell_listbox.delete(0, END)
     
        #ftp connexion
        FTP_PARAMETERS              = CFG_FILE.get('NETWORK_PARAMETERS', SELECTED_LINE)
        SERVER_NAME, PORT, FTP_PATH = FTP_PARAMETERS.split(";")
     
        ftp = ftplib.FTP()
        ftp.connect(SERVER_NAME, int(PORT))
        ftp.login(FTP_USER, FTP_PASSWORD)
     
        Names      = ftp.nlst(FTP_PATH)
        CurrentDir = ftp.pwd()
        SubDirs    = []
     
        for name in Names:
            chemin = name    
            okdir  = GET_Only_directories(ftp, chemin)
            if okdir:
                SubDirs.append(name)
     
        Cell_listbox.delete(0, END)
     
        for SubDir in SubDirs:
            SubDirSplit      = SubDir.split("/")
            LenOfSubDirSplit = len(SubDirSplit)      
            Cell_listbox.insert(END, SubDirSplit[LenOfSubDirSplit-1])
     
        ftp.close()
     
    def FTP_GET_filesinfolder():
        try:
            index = Cell_listbox.curselection()[0]
            CELL_folder = Cell_listbox.get(index)
            print(CELL_folder)
            FTP_PARAMETERS              = CFG_FILE.get('NETWORK_PARAMETERS', SELECTED_LINE)
            SERVER_NAME, PORT, FTP_PATH = FTP_PARAMETERS.split(";")
            ftp = ftplib.FTP()
            ftp.connect(SERVER_NAME, int(PORT))
            ftp.login(FTP_USER, FTP_PASSWORD)
            print('Get files in '+FTP_PATH+CELL_folder)
            MyList = ftp.nlst(FTP_PATH+CELL_folder)
            iFile=0
            FilteredFiles=[]
     
            for file in MyList:
                if file.split('.')[-1]=='txt':
                    continue
                elif file.split('.')[-1]=='io':
                    continue
                else:
                    FilteredFiles.append(file)
                    print(file)
                    iFile+=1
     
            if iFile==1:
                print('One file found')
            elif iFile>1:
                print(iFile, 'files found')
            else:
                print('No file found')
     
            #print(FilteredFiles)
     
        except IndexError:
            return None
     
     
    def FTP_READ_file():
        print('FTP_READ_file')
     
    def GET_Only_directories(ftp, chemin):
        CurrentDir = ftp.pwd()
        try:
            ftp.cwd(chemin)
            ftp.cwd(CurrentDir)
            return True
        except Exception:
            return False
     
    #End of functions
     
    #################################################################################################
    #################################################################################################
     
    #MainGUI
    MainGUI = Tk()
    MainGUI.title(APPLICATION_NAME)
    MainGUI.iconbitmap(r'ini/graph_icon.ico')
    MainGUI.geometry('1200x600')
    Mainfont = font.Font(MainGUI, family='Courier new', size='10')
     
    #LabelFrame for curve selection
    labelframe = LabelFrame(MainGUI, text="SELECTION",  width='20')
    labelframe.grid(padx=5, pady=5)
     
    #Label for ComboBox line choice
    Label_linechoice = Label(MainGUI, text="Select a line:", font=Mainfont).grid(row=0, padx=5, pady=5, sticky=W)
     
    #ComboBox line selection
    ComboBox_linechoice = ttk.Combobox(values=LINE_NAME, state="readonly", font=Mainfont)
     
    #ComboBox_linechoice.set('Select a line')
    ComboBox_linechoice.bind('<<ComboboxSelected>>', lambda e: FTP_GET_folder()) #print(ComboBox_linechoice.get()))
    ComboBox_linechoice.grid(row=1, padx=5)
     
    #Label for ListView cell choice
    Label_cellchoice = Label(MainGUI, text="Select a cell:", font=Mainfont).grid(row=2, padx=5, pady=5, sticky=W)
     
    Cell_listbox = Listbox(MainGUI, font=Mainfont, height=16, width=22)
    Cell_listbox.bind('<<ListboxSelect>>', lambda e: FTP_GET_filesinfolder())
    Cell_listbox.grid(padx=5, column=0, sticky='w')
     
    MainGUI.mainloop()

  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 778
    Points
    36 778
    Par défaut
    Citation Envoyé par Supernatural Voir le message
    Je ne sais pas où mettre le code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Cell_listbox.configure(state=DISABLED)
    et
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Cell_listbox.configure(state=NORMAL)
    ...x
    Certes mais cela n'est pas un problème Python...

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

  5. #5
    Membre régulier
    Homme Profil pro
    Touche à tout
    Inscrit en
    Mai 2017
    Messages
    441
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : Touche à tout

    Informations forums :
    Inscription : Mai 2017
    Messages : 441
    Points : 111
    Points
    111
    Par défaut
    Je suis bien d'accord... merci

    Mon problème est que si je mets
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Cell_listbox.configure(state=NORMAL)
    à la fin de ma fonction FTP_GET_filesinfolder(), ma Listbox n'est jamais vérouillé, alors que si je ne le met pas, ma Listbox est vérouillée mais tout le temps...

    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
    def FTP_GET_filesinfolder():
        try:
            index = Cell_listbox.curselection()[0]
            CELL_folder = Cell_listbox.get(index)
            print(CELL_folder)
            FTP_PARAMETERS              = CFG_FILE.get('NETWORK_PARAMETERS', SELECTED_LINE)
            SERVER_NAME, PORT, FTP_PATH = FTP_PARAMETERS.split(";")
            ftp                         = ftplib.FTP()
            ftp.connect(SERVER_NAME, int(PORT))
            ftp.login(FTP_USER, FTP_PASSWORD)
            print('Get files in '+FTP_PATH+CELL_folder)
            MyList                      = ftp.nlst(FTP_PATH+CELL_folder)
            iFile                       = 0
            FilteredFiles               = []
     
            for file in MyList:
                Cell_listbox.configure(state=DISABLED)
                if file.split('.')[-1]=='txt':
                    continue
                elif file.split('.')[-1]=='io':
                    continue
                else:
                    FilteredFiles.append(file)
                    print(file)
                    iFile+=1
     
            if iFile==1:
                print('One file found')
            elif iFile>1:
                print(iFile, 'files found')
            else:
                print('No file found')
     
            #print(FilteredFiles)
     
        except IndexError:
            return None
     
        Cell_listbox.configure(state=NORMAL)
        ftp.close()

  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 778
    Points
    36 778
    Par défaut
    Salut,

    Citation Envoyé par Supernatural Voir le message
    Mon problème est que si je mets
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Cell_listbox.configure(state=NORMAL)
    à la fin de ma fonction FTP_GET_filesinfolder(), ma Listbox n'est jamais vérouillé, alors que si je ne le met pas, ma Listbox est vérouillée mais tout le temps...
    Je suspecte nombre de problèmes mais je ne vais pas écrire un petit exemple pour peut être reproduire le votre et arriver à des conclusions qui ne serviraient à rien (j'ai mieux à faire).
    Vous pourriez commencer par écrire un petit exemple ou la Listbox est vérouillée/dévérouillée lorsqu'on clique sur des Buttons. Puis vous compliquez la chose pour représenter ce que fait votre application (et éventuellement reproduire le problème dont vous parlez).
    note: normalement vous auriez du faire cet exercice permettant de comprendre comment faire fonctionner votre cas d'utilisation avant de vouloir en intégrer les aspects clefs dans votre code.

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

Discussions similaires

  1. Erreur sur filtre de TCD avec valeur d'une listbox
    Par Aureus Magnus dans le forum Macros et VBA Excel
    Réponses: 9
    Dernier message: 02/02/2015, 22h07
  2. [AC-2010] Listbox, message d'erreur sur mise à jour
    Par GuyDuLac dans le forum VBA Access
    Réponses: 2
    Dernier message: 29/11/2013, 19h33
  3. binding : Effacer contenu listbox
    Par toss.net dans le forum Windows Presentation Foundation
    Réponses: 4
    Dernier message: 24/09/2011, 16h07
  4. Réponses: 4
    Dernier message: 21/04/2011, 16h19

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