Good morning, I have a question about the listbox in tkinter.
My code is working fine, there are only two things wrong and I can not fix it.
First that when I click the Delete button, the program deletes only on time, when I close and reopen the program the line I had deleted reappears again.
and another problem is that from the second time I open the program the whole line is in braces "{}" according to the images below:
Photoofthesecondproblem
followsthecompletecode:
fromtkinterimport*importsqlite3classa:def__init__(self):b=Tk()b.geometry("700x600+75+150")
b.overrideredirect(True)
b["bg"] = "WHITE"
def Fechar(): b.destroy()
cabeçalho = Label(b, background="WHITE")
cabeçalho.grid(row=0, column=0)
self.lin0xcol0 = Label(cabeçalho, text="CONTROLE DE DOCUMENTOS",border=0, relief=FLAT, background="WHITE", width=94)
self.lin0xcol0.grid(row=0, column=0, sticky=N)
self.lin0xcol1 = Button(cabeçalho, width=4, border=0, relief=FLAT, background="RED", cursor="X_cursor", command=Fechar)
self.lin0xcol1.grid(row=0, column=1)
self.linha1 = Label(b, text="Número de Ofício : Assunto : Local : Observação", font=("Arial", 11),border=0, relief=FLAT, background="WHITE", foreground="BLUE")
self.linha1.grid(row=1, column=0)
self.linha2 = Entry(b, width=110, border=1, relief=SOLID)
self.linha2.grid(row=2, column=0)
self.linha3 = Button(b, text="ADICIONAR", width=15, background="BLUE", foreground="WHITE", relief=FLAT, command=self.adicionar)
self.linha3.grid(row=3, column=0, padx=20, pady=8, sticky=E)
c = Label(b, border=0, relief=FLAT, background="WHITE")
c.grid(row=4, column=0)
self.listbox = Listbox(c, width=107, height=25, border=0)
self.listbox.grid(row=0, column=0, pady=10)
#for i in range(100):
# listbox.insert(END, i)
# attach listbox to scrollbar
self.rolagem = Scrollbar(c)
self.rolagem.grid(row=0, column=1, sticky=N+S, pady=10)
self.listbox.config(yscrollcommand=self.rolagem.set)
self.rolagem.config(command=self.listbox.yview)
self.linha4 = Button(b, text="EXCLUIR", width=15, background="RED", foreground="WHITE", relief=FLAT, command=self.apagar)
self.linha4.grid(row=5, column=0, padx=20, sticky=E)
# BANCO DE DADOS
self.conectar = sqlite3.connect("ofícios.db")
self.cursor = self.conectar.cursor()
self.cursor.execute("CREATE TABLE IF NOT EXISTS oficios(oficios TEXT)")
self.conectar.commit()
lista = self.cursor.execute("SELECT * FROM oficios")
for i in lista:
self.listbox.insert(END, i)
def adicionar(self):
oficiosx = self.linha2.get()
self.cursor.execute("insert into oficios values(?)", (oficiosx,))
self.conectar.commit()
self.listbox.insert(END, oficiosx)
def apagar(self):
oficiosy = str(self.listbox.get(ACTIVE))[3:-3]
self.cursor.execute("DELETE FROM oficios WHERE oficios=?", (oficiosy,))
self.conectar.commit()
self.listbox.delete(ANCHOR)
return
b.mainloop()
a()