1
Hello, well, I am writing an application using SQLITE and Python, everything was going well until I had a problem that I am not able to solve, it happens that I have a db of the fipe, so I need to load 3 combobox with mark, model and year, so I decided to do it this way, in the first combobox I carry the marks, then I load the choice with a ".get()", with the reply of ".get()" I would carry the _id in this way:
('SELECT _id FROM tag WHERE name = {}'. format (answered.get())</b
Right after that answer would load the templates with the answer from that select, my problem is that the answer from the ".get()" stays inside my "def Pegamarca(self, Event):" and I’m not being able to use this local variable, I’ve tried to transfer it into a global variable, I’ve tried to add the string by creating an open variable before and then adding it into the function, I’ve tried to do everything that my knowledge is capable but I can’t solve, someone gives me a light, now besides being with the problem I’m curious also to know the solution, I will leave the application code (it has some incomplete parts because I have rewritten it a thousand times to try to solve the problem, but it is better for you to understand my problem).
import tkinter as tk
import tkinter.ttk as ttk
import sqlite3
class Sqlite:
def __init__(self, master):
self.master = master
self.db = sqlite3.connect('fdb.db')
self.cb = ttk.Combobox(master)
self.cb.pack()
self.cb['values'] = self.combo_input()
self.cb.bind("<<ComboboxSelected>>", self.PegarMarca)
self.cc = ttk.Combobox()
self.cc.pack()
self.cc['values'] = self.modelo()
def combo_input(self) -> object:
cursor = self.db.cursor()
cursor.execute('SELECT nome FROM marca')
data = []
for row in cursor.fetchall():
data.append(row[0])
return data
def PegarMarca(self, event):
print(self.cb.get())
def modelo(self) -> object:
cursor = self.db.cursor ()
cursor.execute('SELECT _id FROM marca WHERE nome = "{}"')
data = []
for row in cursor.fetchall():
data.append(row[0])
return data
root = tk.Tk()
Sqlite(root)
root.mainloop()
This class is really weird because it calls
Sqlite
when it is not an instance of Sqlite. It seems to be a form does everything, ie, receives data, manipulates, accesses the database, ie, is anything but a cohesive class. It starts there that makes it easier to have mistakes. That is, fixing the error you are asking would only solve it the wrong way, if it solved. When it starts wrong, the tendency is to try to solve with new mistakes.– Maniero