0

I'm making a python program that registers books in a database. I made two fields that have the auto-complete option, I wanted that whenever a new record was added it would add to the list of these two auto-complete fields the new author and publisher values ​​that were added, but that doesn't happen, someone can help me please? Thank you very much in advance.

Code responsible for taking only the names of registered authors:

def nome_autores(self):
        self.cursor.execute('SELECT autor FROM autores')
        return list(np.array(self.cursor.fetchall()).flatten())

Code responsible for getting only the names of registered publishers:

def nome_editoras(self):
        self.cursor.execute('SELECT editora FROM editoras')
        return list(np.array(self.cursor.fetchall()).flatten())

As I am trying to update the list of autocomplete values:

def adicionar_registro():
    '''
    Esta função é responsável por inserir os valores digitados nos campos de registro da tela
    de registro em questão nas suas devidas tabelas. Caso qualquer dos valores seja um valor
    nulo (valor em branco) ou um valor que não condiza com o seu campo, esta função exibe uma
    mensagem de erro dizendo que algum dos valores digitados está inválido.
    '''
    if drop_down_register.get() == 'Livro':
        titulo = titulo_entry_registro_livro.get().strip()
        autor = autor_entry_registro_livro.get().strip()
        editora = editora_entry_registro_livro.get().strip()
        n_pages = n_pages_entry_registro_livro.get().strip()
        proprietario = proprietario_entry_registro_livro.get().strip()

        if len(titulo) == 0 or len(autor) == 0 or len(editora) == 0 or len(n_pages) == 0 or len(proprietario) == 0 or not n_pages.isdigit():
            messagebox.showinfo('Valores inválidos',
                                'Algum dos valores digitados está inválido!')
        else:
            data_base.add_livro(titulo, autor, editora, n_pages, proprietario)

            titulo_entry_registro_livro.delete(0, END)
            autor_entry_registro_livro.delete(0, END)
            editora_entry_registro_livro.delete(0, END)
            n_pages_entry_registro_livro.delete(0, END)
            proprietario_entry_registro_livro.delete(0, END)

            carrega_tabelas()
            cancelar_registro()

    elif drop_down_register.get() == 'Autor':
        autor = autor_entry_registro_autor.get().strip()

        if len(autor) == 0:
            messagebox.showinfo('Valor inválido', 'Valor digitado inválido')
        else:
            data_base.add_autor(autor)

            autor_entry_registro_autor.delete(0, END)

            carrega_tabelas()
            cancelar_registro()

    elif drop_down_register.get() == 'Editora':
        editora = editora_entry_registro_editora.get().strip()

        if len(editora) == 0:
            messagebox.showinfo('Valor inválido', 'Valor digitado inválido')
        else:
            data_base.add_editora(editora)

            editora_entry_registro_editora.delete(0, END)

            autor_entry_registro_livro['completevalues'] = data_base.nome_autores(
            )
            editora_entry_registro_livro['completevalues'] = data_base.nome_editoras(
            )

            carrega_tabelas()
            cancelar_registro()

autor_entry_registro_livro = AutocompleteCombobox(
    livro_register_frame,
    font='Arial 12',
    completevalues=data_base.nome_autores()
)
autor_entry_registro_livro.grid(row=1, column=1, padx=10, pady=10, sticky=EW)

editora_label_registro_livro = Label(
    livro_register_frame,
    text='Editora',
    font='Arial 12'
)
editora_label_registro_livro.grid(row=2, column=0, padx=10, pady=10)

editora_entry_registro_livro = AutocompleteCombobox(
    livro_register_frame,
    font='Arial 12',
    completevalues=data_base.nome_editoras()
)
editora_entry_registro_livro.grid(row=2, column=1, padx=10, pady=10, sticky=EW)

1 Answers1

0

I was wondering the same thing, and I found this thread to have the solution: How to update values to the listbox under Combobox in ttk Python33 AutocompleteComboBox extends ComboBox anyways, so it makes sense that it works

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 22 '22 at 01:13
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – rachwa Jul 23 '22 at 08:00