0
I’m starting to study sqlite3 in Marra, and this being difficult, but I already got a few things, the problem is that when compiling the program appears the following error:
"name 'adic' is not defined".
Follows the code:
from tkinter import*
import sqlite3
class Principal:
def __init__(self):
janela_principal = Tk()
janela_principal.geometry("400x400+50+50")
#janela_principal.overrideredirect(True)
lb0 = Label(janela_principal)
lb0.grid(row=0, column=0, padx=13)
lb1 = Label(janela_principal, text="Digite um número:")
lb1.grid(row=1, column=1, sticky=E)
digite_numero = Entry(janela_principal, width=35)
digite_numero.grid(row=1, column=2, sticky=W)
separador = Frame(janela_principal, height=2, bd=3, relief=SUNKEN, width=340)
separador.grid(row=2, column=1, columnspan=2)
lb2 = Label(janela_principal, text="Assunto:")
lb2.grid(row=3, column=1, sticky=E)
digite_assunto = Entry(janela_principal, width=35)
digite_assunto.grid(row=3, column=2, sticky=W)
separador = Frame(janela_principal, height=2, bd=3, relief=SUNKEN, width=340)
separador.grid(row=4, column=1, columnspan=2)
add = Button(janela_principal, text="ADICIONAR NOME", relief=SOLID, border=1, foreground="BLUE", command=adic)
add.grid(row=5, column=2, columnspan=2, sticky=W)
apagar = Button(janela_principal, text="APAGAR NOME", relief=SOLID, border=1, foreground="RED", command=apag)
apagar.grid(row=5, column=2, sticky=E)
lb3 = Label(janela_principal)
lb3.grid(row=6, column=0)
rolagem = Scrollbar(janela_principal)
rolagem.grid(row=7, column=2, sticky=N+S+E)
caixa_exibição = Listbox(janela_principal, relief=SOLID, border=1, width=45, height=15, font=("Gentium Basic", 11))
caixa_exibição.grid(row=7, column=1, columnspan=2, sticky=W)
#for i in range(100):
# caixa_exibição.insert(END, i)
# attach listbox to scrollbar
caixa_exibição.config(yscrollcommand=rolagem.set)
rolagem.config(command=caixa_exibição.yview)
#Banco
conectar = sqlite3.connect("numeros.db")
cursor = conectar.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS nomes(numeros TEXT, assunto TEXT)")
conectar.commit()
lista = cursor.execute("SELECT * FROM nomes")
for i in lista:
caixa_exibição.insert(END, i)
def adic():
numerosx = digite_numero.get()
assuntox = digite_assunto.get()
cursor.execute("INSERT INTO nomes values(?, ?)", (numerosx, assuntox))
conectar.commit()
caixa_exibição.insert(END, numerosx)
caixa_exibição.insert(END, assuntox)
def apag():
numerosy = str(caixa_exibição.get(ACTIVE))[3:-3]
assuntoy = str(caixa_exibição.get(ACTIVE))[3:-3]
cursor.execute("DELETE FROM nomes WHERE name=?, ?", (numerosy, assuntoy))
conectar.commit()
caixa_exibição.delete(ANCHOR)
janela_principal.mainloop()
Principal()
How so? You can give me an example?
– VictorS'
Your code is quite confusing: the functions
adicandapagare indented, as if they were class methodsPrincipal, but were defined as functions without the parameterself. Should they be instance methods, class methods or functions outside the class? Also, within the functionadicyou use the objectdigite_numerowhich was defined in__init__and therefore does not exist within this function. Should this object be an instance field? Note that this is not even related to Sqlite yet.– Woss