Escopo
- Desenvolver uma aplicação que disponibilize informações rápidas e objetivas (Tabela na mão) sobre elementos e compostos químicos.
Funcionalidades
- Cadastrar os elementos e compostos químicos
- Alterar dados
- Excluir dados
- Mostrar a Tabela Periódica
- Detalhar os elementos
- Buscar elementos pelo nome
- Nova funcionalidade:
- Criar botão: Metais
- Ao clicar, aparecerão apenas os elementos do Grupo de Metais
Código
<syntaxhighlight lang="python3" line="1">
from tkinter import *
from functools import partial
dicio = {"Nome" : 0, "Símbolo" : 1, "Número Atômico" : 2, "Massa Atômica" : 3, "Grupo" : 4, 0 : "Nome", 1 : "Símbolo", 2 : "Número Atômico", 3 : "Número de Massa", 4 : "Grupo"}
Ler os dados de um elemento
def read() : alt = Tk() alt.title("Dados do Elemento") alt.geometry("350x200+600+250") lb1 = Label(alt, text = "Nome ").place(x = 20, y = 35) lb2 = Label(alt, text = "Símbolo ").place(x = 20, y = 55) lb3 = Label(alt, text = "N° Atômico ").place(x = 20, y = 75) lb4 = Label(alt, text = "Massa Atômica ").place(x = 20, y = 95) lb5 = Label(alt, text = "Grupo ").place(x = 20, y = 115) lista = [] ed1 = Entry(alt) ed2 = Entry(alt) ed3 = Entry(alt) ed4 = Entry(alt) ed5 = Entry(alt) ed1.place(x = 125, y = 35) ed2.place(x = 125, y = 55) ed3.place(x = 125, y = 75) ed4.place(x = 125, y = 95) ed5.place(x = 125, y = 115) ok = Button(alt, width = 6, text = "OK") fechar = Button(alt, width = 6, text = "Fechar", command = alt.destroy) def get_lista() : lista.append(ed1.get() + ' ') lista.append(ed2.get() + ' ') lista.append(ed3.get() + ' ') lista.append(ed4.get() + ' ') lista.append(ed5.get() + '\n') alt.quit() ok.place(x = 215, y = 120) fechar.place(x = 215, y = 150) ok["command"] = get_lista alt.mainloop() alt.destroy() return lista
Mostra um elemento
def show(elemento) : jan = Tk() jan.title(elemento[0]) jan["bg"] = "pink" jan.geometry("300x200+600+250") for i in range(5) : Label(jan, text = dicio[i] + ": " + elemento[i], bg = jan["bg"], font = ("Arial", 14)).pack() bt = Button(jan, text = "Fechar", command = jan.quit).pack() jan.mainloop() jan.destroy()
Testa se senha é valida
def senha() : alt = Tk() alt.title("Senha") alt.geometry("300x200+600+250") lb = Label(alt, text = "Senha ") ed = Entry(alt, show = "*") lb.place(x = 20, y = 75) ed.place(x = 70, y = 75) ok = Button(alt, width = 6, text = "OK") fechar = Button(alt, width = 6, text = "Fechar", command = alt.destroy) cont=0 def get_senha() : nome = ed.get() if nome != "querocriar123": Label(alt, text = "Senha incorreta").place(x = 20, y = 105) else : alt.quit() ok.place(x = 160, y = 105) fechar.place(x = 160, y = 135) ok["command"] = get_senha alt.mainloop() alt.destroy()
Adiciona um elemento
def create(): senha() elemento = read() aux = elemento[0] + elemento[1] + elemento[2] + elemento[3] if len(aux) > 4: arquivo = open('dados.txt', 'r') flag = True texto = arquivo.readlines() arquivo.close() for i in elemento: for j in texto: lista = j.split() cont = 0 for k in lista: if cont < 3: if i == (k + ' '): flag = False if flag == False: break else: break cont += 1 if flag == False: break if flag == False: break arquivo = open('dados.txt', 'a') if flag == True: arquivo.writelines(elemento) else: jan = Tk() jan.title("Elemento Existente") jan["bg"] = "lightyellow" jan.geometry("350x200+600+250") Label(jan, text = "O elemento " + elemento[0] + "já existe", font = ("Arial", 13, "bold"), bg = jan["bg"]).place(x = 50, y = 55) bt = Button(jan, text = "Fechar", command = jan.quit).place(x = 135, y = 105) jan.mainloop() jan.destroy() arquivo.close()
Consulta um elemento
def recover() : alt = Tk() alt.title("Consultar") alt["bg"] = "lightblue" alt.geometry("300x200+600+250") Label(alt, text = "A consulta pode ser realizada por:", font = ("Arial", 11, "bold"), bg = alt["bg"]).place(x = 25, y = 15) Label(alt, text = " Nome", font = ("Arial", 10, "bold"), bg = alt["bg"]).place(x = 0, y = 35) Label(alt, text = " Símbolo", font = ("Arial", 10, "bold"), bg = alt["bg"]).place(x = 0, y = 55) Label(alt, text = " N° Atômico", font = ("Arial", 10, "bold"), bg = alt["bg"]).place(x = 0, y = 75) Label(alt, text = " Massa Atômica", font = ("Arial", 10, "bold"), bg = alt["bg"]).place(x = 0, y = 95) Label(alt, text = " Grupo", font = ("Arial", 10, "bold"), bg = alt["bg"]).place(x = 0, y = 115) ed = Entry(alt) ed.place(x = 58, y = 150) ok = Button(alt, width = 5, text = "OK") ok.place(x = 58, y = 180) fechar = Button(alt, width = 6, text = "Fechar", command = alt.destroy) fechar.place(x = 138, y = 180)
#----------------Função que procura por um elemento
def busca() :
nome = ed.get()
if len(nome) > 0:
arquivo = open('dados.txt', 'r')
texto = arquivo.readlines()
for linha in texto :
lista = linha.split()
flag = False
cont = 0
for x in lista :
if cont == 3 : break
if x == nome :
flag = True
break
cont += 1
if flag == True :
show(lista)
break
else :
jan = Tk()
jan.title("Elemento não encontrado")
jan["bg"] = "lightyellow"
jan.geometry("350x200+600+250")
arquivo.close()
Label(jan, text = "Deseja criar o elemento " + nome, font = ("Arial", 13, "bold"), bg = jan["bg"]).place(x = 50, y = 35)
ans1 = Button(jan, width = 5, text = "SIM", command = create).place(x = 130, y = 75)
ans2 = Button(jan, width = 5, text = "NAO", command = jan.destroy).place(x = 130, y = 105)
jan.mainloop()
jan.destroy()
arquivo.close()
ok["command"] = busca alt.mainloop() alt.destroy()
Atualiza um elemento
def update() : alt = Tk() alt.title("Atualizar") alt["bg"] = "lightyellow" alt.geometry("300x200+600+250") Label(alt, text = "A pesquisa pode ser realizada por:", font = ("Arial", 11, "bold"), bg = alt["bg"]).place(x = 25, y = 15) Label(alt, text = " Nome", font = ("Arial", 10, "bold"), bg = alt["bg"]).place(x = 0, y = 35) Label(alt, text = " Símbolo", font = ("Arial", 10, "bold"), bg = alt["bg"]).place(x = 0, y = 55) Label(alt, text = " N° Atômico", font = ("Arial", 10, "bold"), bg = alt["bg"]).place(x = 0, y = 75) Label(alt, text = " Massa Atômica", font = ("Arial", 10, "bold"), bg = alt["bg"]).place(x = 0, y = 95) ed = Entry(alt) ed.place(x = 58, y = 125) ok = Button(alt, width = 6, text = "OK") ok.place(x = 58, y = 150) fechar = Button(alt, width = 6, text = "Fechar", command = alt.destroy) fechar.place(x = 138, y = 150) aux = ""
#----------------Função que procura por um elemento para o atualizar def muda(): nome = ed.get() if len(nome) > 0 : arquivo = open('dados.txt', 'r') texto = arquivo.readlines() arquivo.close() arquivo = open('dados.txt', 'a') flag = False for linha in texto : lista = linha.split() flag1 = False cont = 0 for x in lista : if cont == 3 : break if x == nome : flag = flag1 = True break cont += 1 if flag1 == True : aux = lista[0] + " " + lista[1] + " " + lista[2] + " " + lista[3] + "\n" show(lista) lista = read() arquivo.writelines(lista) cont += 1 if flag == False : jan = Tk() jan.title("Elemento não encontrado") jan["bg"] = "lightyellow" jan.geometry("350x200+600+250") arquivo.close() Label(jan, text = "Deseja criar o elemento " + nome, font = ("Arial", 13, "bold"), bg = jan["bg"]).place(x = 50, y = 35) ans1 = Button(jan, width = 5, text = "SIM", command = create).place(x = 130, y = 75) ans2 = Button(jan, width = 5, text = "NAO", command = jan.destroy).place(x = 130, y = 105) jan.mainloop() jan.destroy()
arquivo = open('c:\dados.txt', 'r') texto = arquivo.readlines() arquivo.close() arquivo = open('dados.txt', 'w') for i in texto: if aux != i : arquivo.writelines(i) arquivo.close()
ok["command"] = muda alt.mainloop() alt.destroy()
Deleta um elemento
def delete() : jan = Tk() jan.title("Deletar") jan["bg"] = "lightyellow" jan.geometry("300x200+600+250") lb = Label(jan, text = "Digite o nome do elemento:", font = ("Arial", 13, "bold"), bg = jan["bg"]) bt = Button(jan, width = 6, text = "OK") fecha = Button(jan, width = 6, text = "Fechar", command = jan.destroy) ed = Entry(jan)
#----------------Função que procura por um elemento para o excluir def excluir(): nome = ed.get() if len(nome) > 0 : arquivo = open('dados.txt', 'r') texto = arquivo.readlines() arquivo.close() arquivo = open('dados.txt', 'w') cont = 0 flag = False deleted = [] for linha in texto : lista = linha.split() flag1 = False cont = 0 for x in lista : if cont == 3 : break if x == nome : flag = flag1 = True break cont += 1 if flag1 == False : arquivo.writelines(linha) else : deleted = lista cont += 1 if flag == True : alt = Tk() alt.title("Deletar") alt["bg"] = "lightyellow" alt.geometry("350x220+600+250") Label(alt, text = "O elemento " + deleted[0] + " foi deletado.", font = ("Arial", 13, "bold"), bg = alt["bg"]).place(x = 30, y = 70) else : jan = Tk() jan.title("Elemento não encontrado") jan["bg"] = "lightyellow" jan.geometry("350x200+600+250") arquivo.close() Label(jan, text = "Deseja criar o elemento " + nome, font = ("Arial", 13, "bold"), bg = jan["bg"]).place(x = 50, y = 35) ans1 = Button(jan, width = 5, text = "SIM", command = create).place(x = 130, y = 75) ans2 = Button(jan, width = 5, text = "NAO", command = jan.destroy).place(x = 130, y = 105) jan.mainloop() jan.destroy()
lb.place(x = 30, y = 55) ed.place(x = 69, y = 85) bt.place(x = 75, y = 110) fecha.place(x = 155, y = 110) bt["command"] = excluir jan.mainloop() jan.destroy()
Janela principal
root = Tk() root.title("HandTable") root["bg"] = "lightblue" root.geometry("900x500+250+150") Label(root, text = "Bem vindo ao HandTable! \n Aqui você pode encontrar as informações da tabela periódica de forma rápida e fácil! \n A tabela em sua mão!", font = ("Century", 15, "bold"), bg = root["bg"]).place(x = 10, y = 40) criar = Button(root, text = "Criar", width = 8, command = create).place(x = 450, y = 180) atualizar = Button(root, text = "Atualizar", width = 8, command = update).place(x = 450, y = 210) deletar = Button(root, text = "Deletar", width = 8, command = delete).place(x = 450, y = 240) consultar = Button(root, text = "Consultar", width = 8, command = recover).place(x = 450, y = 270) reações = Button (root, text = "Reações", width = 8, command = recover) .place (x = 450, y = 300) metais = Button (root, text = "Metais", width = 8, command = recover) .place (x = 450, y = 330) fechar = Button(root, text = "Fechar", width = 8, command = quit).place(x = 450, y = 360) root.mainloop() root.destroy() </syntaxhighlight>