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()