I have a little problem with listbox - in Tkinter

Asked

Viewed 361 times

1

Good morning, I have a question about the listbox on Tkinter.

my code is working fine, there’s only two things wrong and I’m not getting it fixed.

first that when I click the delete button, the program deletes only on time, when I close and reopen the program the row I had deleted reappears again.

and another problem is that from the second time I open the program the whole line is between keys "{ }" as the images below:

Photo of the first problem inserir a descrição da imagem aqui

Photo of the second problem inserir a descrição da imagem aqui

follows the full code:

from tkinter import*
import sqlite3

class a:
    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()
  • Good morning ! Have you thought about using ttk.treeview ? In my opinion it would be better for your program

  • Unfortunately with treeview the same thing happens. I believe that if I can solve with listbox, it will be valid even for treeview as well.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.