· 6 years ago · Oct 20, 2019, 05:27 AM
1from tkinter import*
2import sqlite3
3##Globais###
4conn = sqlite3.connect("int.db")
5cursor = conn.cursor()
6Janela = Tk()
7def criarTabela():
8 cursor.execute("""
9 CREATE TABLE IF NOT EXISTS alunos(
10 matricula INTEGER NOT NULL PRIMARY KEY,
11 nome TEXT NOT NULL,
12 nota TEXT NOT NULL
13 );
14 """)
15criarTabela()
16def add_aluno():
17 matricula=etmat.get()
18 nome = etnome.get()
19 nota = etnota.get()
20 cursor.execute("""INSERT INTO alunos (matricula, nome, nota) VALUES (?,?,?)""", (matricula, nome, nota))
21 conn.commit()
22 lstAlunos.insert(END, (matricula, nome, nota))
23
24def deletar_estudante():
25 matricula_aluno = entry_matricula_cancelar.get()
26 cursor.execute("""DELETE FROM alunos WHERE matricula=?""",
27 (matricula_aluno,))
28 conn.commit()
29 lstAlunos.delete(0, END)
30 lista = cursor.execute("""SELECT * FROM alunos;""")
31 for i in lista:
32 lstAlunos.insert(END, i)
33
34def mudarNota():
35 matricula_aluno = entry_MatriculaMudar.get()
36 nota_nova = entry_NovaNota.get()
37 cursor.execute('''UPDATE alunos SET nota = ? WHERE matricula = ? ''', (nota_nova, matricula_aluno))
38 conn.commit()
39 lstAlunos.delete(0, END)
40 lista = cursor.execute('''SELECT * FROM alunos;''')
41 for i in lista:
42 lstAlunos.insert(END, i)
43
44
45
46
47#widget principal
48lb_titulo = Label(Janela, text = 'Cadastro:', bg = 'teal', fg = 'white', font = 'Ubuntu 13 bold')
49lb_titulo.place(x=370, y = 2)
50lb_nome_nota = Label(Janela, text = 'Matricula / Nome / Nota', bg = 'teal', fg = 'white', font = 'Ubuntu 12 bold')
51lb_nome_nota.place(x=370, y =25)
52
53#widget add aluno
54label_add_aluno = Label(Janela, text = 'Adicionar aluno:', bg = 'teal', fg = 'white', font = 'Ubuntu 12 bold')
55label_add_aluno.place(x=160, y=10)
56
57label_matricula = Label(Janela, text = 'Adicionar Matricula', bg = 'teal', fg = 'white', font = 'Ubuntu 10 bold')
58label_matricula.place(x=10, y=55)
59
60label_nome = Label(Janela, text = 'Adicionar nome = ', bg = 'teal', fg = 'white', font = 'Ubuntu 10 bold')
61label_nome.place(x=10, y=100)
62
63label_nota = Label(Janela, text = 'Adicionar Nota', bg = 'teal', fg = 'white', font = 'Ubuntu 10 bold')
64label_nota.place(x=10, y=140)
65
66add_aluno = Button(Janela, text = "Adicionar", bg = 'green', fg = 'white', font = 'Ubuntu 10 bold', command = add_aluno)
67add_aluno.place(x = 20, y = 180)
68
69etmat = Entry(Janela)
70etmat.place(x=160, y=52)
71etnome = Entry(Janela)
72etnome.place(x=160, y=100)
73etnota = Entry(Janela)
74etnota.place(x=160, y=140)
75
76#widgets deletar aluno#
77
78label_titulo_deletar=Label(Janela, text = 'Deletar aluno:', bg = 'teal', fg = 'white', font = 'Ubuntu 12 bold')
79label_titulo_deletar.place(x=160, y =230)
80label_mat_cancelar = Label(Janela, text = 'Matricula', bg = 'teal',fg = 'white', font = 'Ubuntu 10 bold')
81label_mat_cancelar.place(x = 10, y = 270)
82entry_matricula_cancelar = Entry(Janela)
83entry_matricula_cancelar.place(x = 160, y = 270)
84botao_deletar = Button(Janela, text ='Deletar', bg = "red", fg = 'white', font = 'Ubuntu 10 bold', command = deletar_estudante)
85botao_deletar.place(x=340, y = 270)
86# widget listar alunos
87scrollbar = Scrollbar(Janela)
88lstAlunos = Listbox(Janela, width=27, height=10)
89lstAlunos.config(yscrollcommand=scrollbar.set)
90scrollbar.config(command = lstAlunos.yview)
91lista = cursor.execute("""
92 SELECT *FROM alunos;""")
93for i in lista:
94 lstAlunos.insert(END, i)
95lstAlunos.place(x=354,y=45)
96scrollbar.place()
97#WIdget MUDAR NOTA#
98MudarNota = Label(Janela, text = 'Mudar Nota:', bg = 'teal',fg = 'white', font = 'Ubuntu 12 bold')
99MudarNota.place(x= 160, y=340)
100MatriculaMudar = Label(Janela, text = 'Mudar Matricula:', bg = 'teal',fg = 'white', font = 'Ubuntu 10 bold')
101MatriculaMudar.place(x= 10, y=370)
102entry_MatriculaMudar = Entry(Janela)
103entry_MatriculaMudar.place(x =130, y=370)
104NovaNota = Label(Janela, text = 'Nova Nota:', bg = 'teal',fg = 'white', font = 'Ubuntu 10 bold')
105NovaNota.place(x= 10, y=400)
106entry_NovaNota= Entry(Janela)
107entry_NovaNota.place(x =130, y=400)
108but_mudar_nota = Button(Janela,text = 'Alterar', bg = "gold", fg = 'gray', font = 'Ubuntu 10 bold', command = mudarNota)
109but_mudar_nota.place(x = 320, y=380)
110#fim#
111
112Janela.geometry('600x500')
113Janela.title('Cadastrar Aluno')
114Janela.configure(bg = 'teal')
115Janela.resizable(FALSE, FALSE)
116Janela.mainloop()