I’m having trouble with the code

Asked

Viewed 53 times

-4

people I am making a school system with python using the modules Tkinter and Sqlite but when I insert the variables of the text boxes in the database information it gives this problem

...

take a look at my script

from tkinter import *
import sqlite3
import sys
def bt1_click():
print('Nome do aluno: ',ed1.get())
print('Nome do responsavel: ',ed2.get())
print('Numero de contato: ',ed3.get())
print('Ano de escolariade: ',ed4.get())
print('data de naiscimento: ',ed5.get())
print('O aluno apresenta alguma deficiencia ?: ',texto.get("1.0",END))
root = Tk()
lb1= Label(text='nome do aluno: ')
lb2= Label(text='Nome do responsavel: ')
lb3= Label(text='Numero de contato: ')
lb4= Label(text='Ano de escolariade: ')
lb5= Label(text='data de naiscimento: ')
lb6= Label(text='Idade do aluno: ')
lb7= Label(text='O aluno apresenta alguma deficiencia ?: ')
lb8= Label(text='')

lb1.grid(row=1, column=0)
lb2.grid(row=3, column=0)
lb3.grid(row=5, column=0)
lb4.grid(row=7, column=0)
lb5.grid(row=10, column=0)
lb6.grid(row=9, column=0)
lb7.grid(row=11, column=0)
lb8.grid(row=13,column=0)

ed1= Entry('')
ed2= Entry('')
ed3= Entry('')
ed4= Entry('')
ed5= Entry('')
ed6= Entry('')
texto = Text('', height = 13, width = 20)

ed1.grid(row=2,column=0)
ed2.grid(row=4,column=0)
ed3.grid(row=6,column=0)
ed4.grid(row=8,column=0)
ed5.grid(row=10,column=0)
texto.grid(row=12,column=0)

bt1 = Button(text='enviar ao banco de dados', command=bt1_click)



bt1.grid(row=14,column=0)


root.geometry('220x500+100+100')

root.title('Ssystem')

root.mainloop()

ee1 = ed1.get()
ee2 = ed2.get()
ee3 = ed3.get()
ee4 = ed4.get()
ee5 = ed5.get()
ee6 = ed6.get()


banco = sqlite3.connect('SchoolSystem_Users.db')#objeto de conexao com o banco

cursor = banco.cursor()

#cursor.execute("CREATE TABLE users (Nome do aluno, Nome do responsavel, numero do responsavel,ano de escolaridade,data de naiscimento,idade do aluno)")

cursor.execute("INSERT INTO users VALUES ('ee1''ee2''ee3''ee4''ee5''ee5')")

banco.commit()

""" cursor.execute('''SELECT * FROM users ''')
print(cursor.fetchall()) """

----------


==========
  • Would you like to check the indentation of the code you pasted? It is wrong, and then we cannot know what is part of a function and what is not. Also, if you have more than one archive, put it in separate blocks - you have strange symbols there that would be syntax error in Python (row of ----- and of ====) - And finally, your database code on that list will never run, since it’s after the "mainloop" call. Put down a list that works, and give the error you want to clarify.

1 answer

1

This error occurs because you tried to obtain the contents of a variable after it was destroyed, mainloop() serves to make the program run in loop, if you add some instruction after the mainloop() Ex.: print('Olá') this will only occur after the user closes the program, and after the program is closed the parent window (in this case root) ceases to exist, as well as all others that have been defined as her daughters, Inputs, Buttons, Widgets, etc.

There are also 2 simple problems in your code:

  1. You forgot to write the function bt1_click()
  2. You entered the characters ---------- and ========== at the end of the line, python will generate an error message, because it does not recognize these characters, you can put them as comments.

Your code the right way would look like this:

from tkinter import *
import sqlite3
import sys

def bt1_click():
    # Definindo as variáveis como sendo globais
    # Ou seja, elas poderão ser acessadas mesmo após a função bt1_click ser finalizada
    global ee1
    global ee2
    global ee3
    global ee4
    global ee5
    global ee6

    ee1 = ed1.get()
    ee2 = ed2.get()
    ee3 = ed3.get()
    ee4 = ed4.get()
    ee5 = ed5.get()
    ee6 = ed6.get()

    print('Nome do aluno: ', ee1)
    print('Nome do responsavel: ', ee2)
    print('Numero de contato: ', ee3)
    print('Ano de escolariade: ', ee4)
    print('data de naiscimento: ', ee5)
    print('O aluno apresenta alguma deficiência ?: ',texto.get("1.0",END))



root = Tk()
lb1 = Label(text = 'Nome do aluno: ')
lb2 = Label(text = 'Nome do responsavel: ')
lb3 = Label(text = 'Numero de contato: ')
lb4 = Label(text = 'Ano de escolariade: ')
lb5 = Label(text = 'data de naiscimento: ')
lb6 = Label(text = 'Idade do aluno: ')
lb7 = Label(text = 'O aluno apresenta alguma deficiencia ?: ')
lb8 = Label(text = '')

lb1.grid(row = 1, column = 0)
lb2.grid(row = 3, column = 0)
lb3.grid(row = 5, column = 0)
lb4.grid(row = 7, column = 0)
lb5.grid(row = 10, column = 0)
lb6.grid(row = 9, column = 0)
lb7.grid(row = 11, column = 0)
lb8.grid(row = 13, column = 0)

ed1 = Entry('')
ed2 = Entry('')
ed3 = Entry('')
ed4 = Entry('')
ed5 = Entry('')
ed6 = Entry('')
texto = Text('', height = 13, width = 20)

ed1.grid(row = 2, column = 0)
ed2.grid(row = 4, column = 0)
ed3.grid(row = 6, column = 0)
ed4.grid(row = 8, column = 0)
ed5.grid(row = 10, column = 0)
texto.grid(row = 12, column = 0)

bt1 = Button(text = 'enviar ao banco de dados', command = bt1_click)
bt1.grid(row = 14,column = 0)

root.geometry('220x500+100+100')
root.title('Ssystem')
root.mainloop()

# Printando todas as variáveis com os valores para garantir que esteja funcionando

print(ee1)
print(ee1)
print(ee2)
print(ee3)
print(ee4)
print(ee5)

banco = sqlite3.connect('SchoolSystem_Users.db')#objeto de conexao com o banco

cursor = banco.cursor()

#cursor.execute("CREATE TABLE users (Nome do aluno, Nome do responsavel, numero do responsavel,ano de escolaridade,data de naiscimento,idade do aluno)")

cursor.execute("INSERT INTO users VALUES ('ee1''ee2''ee3''ee4''ee5''ee5')")

banco.commit()

""" cursor.execute('''SELECT * FROM users ''')
print(cursor.fetchall()) """

Browser other questions tagged

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