I get error messages like
_tkinter.TclError: bad listbox index "": must be active, anchor, end, @x,y, or a number
init__.py", line 3187, in get
return self.tk.call(self._w, 'get', first)
and I have no clue how to fix this, tried looking online but I'm just lost
def vindu_2():
def hent_eksamner():
sok_eksam = lst_eksamen.get(lst_eksamen.curselection())
data_eksamen = mindatabase.cursor()
data_eksamen.execute('SELECT * FROM Eksamen')
for row in data_eksamen:
if sok_eksam == row[1]:
emnekode.get(row[0])
romnr.get(row[2])
data_eksamen.close()
eksamen = []
vindu2=Toplevel()
vindu2.title('Eksamner dag')
y_scroll = Scrollbar(vindu2, orient=VERTICAL)
y_scroll.grid(row=0, column=2, rowspan=10, padx=(0,100), pady=5, sticky=NS)
innhold_i_lst_eksamen=StringVar()
lst_eksamen = Listbox(vindu2, width=50, height=10, listvariable=innhold_i_lst_eksamen, yscrollcommand=y_scroll.set)
lst_eksamen.grid(row=0, column=1, rowspan=10, padx=(100,0), pady=5, sticky=E)
innhold_i_lst_eksamen.set(tuple(eksamen))
y_scroll['command']=lst_eksamen.yview
lbl_emnekode = Label(vindu2, text='Emnekode: ')
lbl_emnekode.grid(row=0, column=3, padx=5, pady=5, sticky=E)
lbl_dato = Label(vindu2, text='Dato: ')
lbl_dato.grid(row=1, column=3, padx=5, pady=5, sticky=E)
emnekode = StringVar()
ent_emnekode = Entry(vindu2, width=10, state='readonly', textvariable = emnekode)
ent_emnekode.grid(row=0, column=4, padx=5, pady=5, sticky=W)
dato = StringVar()
ent_dato = Entry(vindu2, width=10, state='readonly', textvariable = dato)
ent_dato.grid(row=1, column=4, padx=5, pady=5, sticky=W)
romnr = StringVar()
ent_romnr = Entry(vindu2, width=10, state='readonly', textvariable = romnr)
ent_romnr.grid(row=2, column=4, padx=5, pady=5, sticky=W)
sok_eksam = StringVar()
ent_sok_eksam = Entry(vindu2, width=10, textvariable = sok_eksam)
ent_sok_eksam.grid(row=3, column=4, padx=5, pady=5, sticky=W)
btn_sok_eksam = Button(vindu2, width=5, text='Søk', command = hent_eksamner)
btn_sok_eksam.grid(row=4, column=4, padx=5, pady=5, sticky=W)
lst_eksamen.bind('<<ListboxSelect>>', hent_eksamner)
btn_tilbake2 = Button(vindu2, text='Tilbake til meny', command = vindu2.destroy)
btn_tilbake2.grid(row=6, column=4, padx=5, pady=25, sticky=E)
CodePudding user response:
lst_eksamen.curselection()
returns a list of all the indices in the current listbox selection so the bad index is this list - not an index.
print(lst_eksamen.curselection())
will make this clear and will show which index in the curselection you are wanting. It is done this way for when there is more than one item in the listbox selected.
If there is only one selection then
sok_eksam = lst_eksamen.get(lst_eksamen.curselection()[0])
should work. The index is the first item in the curselection
list.
Personally I would place the selection index into a separate variable for readability if performance is not an issue.eg
lst_index = lst_eksamen.curselection()[0]
sok_eksam = lst_eksamen.get(lst_index)
CodePudding user response:
It is because lst_eksamen.curselection()
returns a empty tuple due to no item is selected. You need to check whether item is selected to determine whether to proceed or not.
Also there is another issue that hent_eksamner()
is used as the callback for both a button command and an event binding. When it is called by the event binding, exception will be raised because it is expected that the function should accept an argument, the Event
object.
Below is the modified hent_eksamner()
to fix the above issues:
# added the event argument with default value to support
# button command and event binding
def hent_eksamner(event=None):
selection = lst_eksamen.curselection()
# check whether there is item selected
if selection:
sok_eksam = lst_eksamen.get(selection)
data_eksamen = mindatabase.cursor()
data_eksamen.execute('SELECT * FROM Eksamen')
for row in data_eksamen:
if sok_eksam == row[1]:
emnekode.get(row[0])
romnr.get(row[2])
data_eksamen.close()