I have created two functions that must start at the same time as soon as I select the items of a combobox. I tried to make them start together by writing: combo_Campionato ['values'] = combo_campionato (), filter_campionato (). The first function by importing the combobox items. The second function ensures that, by selecting an item in the combobox, the rows displayed in the grid are "filtered" according to the selected combobox item.
But I get this error:
combo_Campionato['values'] = combo_campionati(), filtra_campionato()
TypeError: filtra_campionato() missing 1 required positional argument: 'campionato'
If I delete the name of the second function, and leave only the first one working, the script starts correctly: combo_Campionato ['values'] = combo_campionato ()
This is the code:
def combo_campionati():
campionato = combo_Campionato.get()
cursor.execute('SELECT Nome_Campionato FROM ARCHIVIO_CAMPIONATI')
result=[row[0] for row in cursor]
return result
def filtra_campionato(campionato):
campionato = combo_Campionato.get()
cursor.execute('SELECT * FROM ARCHIVIO_Risultati WHERE campionato=?',(campionato,))
result=[row[0] for row in cursor]
return results
#Combobox Campionati
lbl_Campionato = Label(root, text="Campionato", font=("Calibri", 11), bg="#E95420", fg="white")
lbl_Campionato.place(x=6, y=60)
combo_Campionato = ttk.Combobox(root, font=("Calibri", 11), width=30, textvariable=campionato, state="readonly")
combo_Campionato.place(x=180, y=60)
combo_Campionato.set("Seleziona campionato")
combo_Campionato['values'] = combo_campionati(), filtra_campionato()
combo_Campionato.bind('<<ComboboxSelected>>', combo_squadre)
I don't know if I'm moving on in the right way. If I'm wrong, how can I call those two functions at the same time when I select the combobox items? Thanks
CodePudding user response:
The reason you get an error is - You need to pass a variable inside the function filtra_campionato
. For e.g. - something like - filtra_campionato(something)
So, instead of this -
def filtra_campionato(campionato):
campionato = combo_Campionato.get()
cursor.execute('SELECT * FROM ARCHIVIO_Risultati WHERE campionato=?',(campionato,))
result=[row[0] for row in cursor]
return results
Remove the campionato
in the () because it is not needed -
def filtra_campionato():
campionato = combo_Campionato.get()
cursor.execute('SELECT * FROM ARCHIVIO_Risultati WHERE campionato=?',(campionato,))
result=[row[0] for row in cursor]
return result
You need - return result
not return results
because it is singular in the result=[row[0] for row in cursor]
. Python interprets it as different variable.