Bonjour,

Par une combobox, je souhaite sélectionner un auteur par exemple "Platon" dans ma liste déroulante provenant de ma table [tb_auteur] pour lancer ensuite une recherche dans ma tb_citation et extraire toutes les citations de "Platon".

Nom : 020.JPG
Affichages : 174
Taille : 28,4 Ko

Un peu comme cela.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
 
def callbackFunc(event):
    print(comboExample.get())
 
app = tk.Tk()
app.geometry('200x100')
labelTop = tk.Label(app,text="Choose your favourite month")
labelTop.grid(column=0, row=0)
comboExample = ttk.Combobox(app,values=["Socrate","Platon","Plutarque","Chruchill"])
comboExample.grid(column=0, row=1)
comboExample.bind("<<ComboboxSelected>>", callbackFunc)
 
app.mainloop()
Mais cela ne marche pas... il me manque une compréhension.
Pourriez-vous s'il vous plaît m'aider ?
Merci beaucoup par avance.

Voici 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
# simple criteria or multi-criteria search functions

# function callback
def callbackAuteur(event):
    print(cmb_attribution.get())  # DEBUG
rslcmb = cmb_attribution.get()


def cmb_auteur2():
    """ Data recovery and insertion in combobox author_citation """
connexion = sqlite3.connect('mnesis.db')
    cursor = connexion.cursor()
    cursor.execute('SELECT auteur FROM tb_auteur ORDER BY auteur')
    data1 = []
    for row in cursor.fetchall():
        data1.append(row[0])
    return data1


def queryQuoteforauteur():
    """ Returns the values of the records of the Database """
connexion = sqlite3.connect('mnesis.db')
    cursor = connexion.cursor()
    rsl = rslcmb
    cursor.execute('SELECT * FROM tb_citation WHERE auteur=?', (rsl,))
    print(f"SELECT * FROM tb_citation WHERE auteur = '{rsl}'")  # DEBUG
resultDatabase = cursor.fetchall()
    results = ''
for row in resultDatabase:
        results += '\n'.join({
            'Auteur: {}'.format(row[1]),
'Citation: {}'.format(row[2]),
'Référence: {}'.format(row[3]),
'-----------------------\n'
})
    connexion.close()
    return results


def result_quote():
    """ Returns the values of the database records """
tpl_result = Toplevel()  # == Contructor Toplevel ==
tpl_result.title(" Data display")
    tpl_result.geometry("1024x600")
    # Text area with scrollbar
zonetext = tk.Text()
    zonetext = ScrolledText(tpl_result, width=120, height=35)
    zonetext.pack()
    resulreq = query_quote()
    zonetext.insert("0.0", resulreq)
    # end of the loop ---
tpl_result.mainloop()


def search_data():
    """
    Search by selection of a combobox
    """
global tpl_search

    tpl_search = Toplevel()  # == Constructor Toplevel ==
tpl_search.title(" Search by selection of a combobox")
    screen_x = int(tpl_search.winfo_screenwidth())
    screen_y = int(tpl_search.winfo_screenheight())
    tpl_search_x = 400
tpl_search_y = 100
pos_x = (screen_x // 2) - (tpl_search_x // 2)
    pos_y = (screen_y // 2) - (tpl_search_y // 2)
    geo = "{}x{}+{}+{}".format(tpl_search_x, tpl_search_y, pos_x, pos_y)
    tpl_search.geometry(geo)
    tpl_search.resizable(width=False, height=False)  # Editable window True or False
tpl_search.configure(bg='Gray79')
    # ------------
attribution_label = Label(tpl_search, text="Author", bg='Gray79', font=("Arial", 11, "bold"))
    attribution_label.place(x=100, y=25)

    cmb_attribution = ttk.Combobox(tpl_search, values=cmb_auteur2(), width=20, height=30,
font=("Arial", 10, "bold"))
    cmb_attribution.bind('<<ComboboxSelected>>', cmb_auteur2)
    cmb_attribution.place(x=100, y=46)
    # ----
record_btn = Button(tpl_search, text='Launch', activebackground="SkyBlue1", font=("Arial", 8),
command=lambda: queryQuoteforauteur())
    record_btn.place(x=280, y=46)
    # end of the loop ---
tpl_search.mainloop()
A la sélection de l'auteur j'ai ce message :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
 
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\xxxxxxx\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
TypeError: cmb_auteur2() takes 0 positional arguments but 1 was given
Et quand je lance la recherche j'ai ce message :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
 
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\xxxxxx\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/xxxxxxxx/Documents/_PythonDeveloppement/GnosisProject/main21_b.py", line 986, in <lambda>
    command=lambda: queryQuoteforauteur())
  File "C:/Users/xxxxxx/Documents/_PythonDeveloppement/GnosisProject/main21_b.py", line 927, in queryQuoteforauteur
    rsl = rslcmb
NameError: name 'rslcmb' is not defined