Last active
July 29, 2022 03:52
-
-
Save dhelbegor/62a3598f4c595bbe5686 to your computer and use it in GitHub Desktop.
Sistema simples para cadastro de produtos, incluindo cadastro de usuarios!!!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:UTF-8 -*- | |
from ttk import * | |
from Tkinter import * | |
import tkMessageBox | |
import sqlite3 | |
import ttk | |
import os | |
raiz=Tk() | |
def dic_bd(bd, tabela): | |
conect=sqlite3.connect(bd) | |
c = conect.cursor() | |
a={} | |
for i in c.execute("SELECT * FROM %s" %tabela): | |
a[i[0]]=[i[1],i[2]] | |
conect.close() | |
return a | |
#Criando banco de dados | |
conectar = sqlite3.connect("data/Control_base.db") | |
c = conectar.cursor() | |
c.execute("""CREATE TABLE IF NOT EXISTS produtos(id_produto INTEGER PRIMARY KEY AUTOINCREMENT, produto TEXT,ean REAL,ncm REAL, cst1 REAL, cst2 REAL)""") | |
c.execute("""CREATE TABLE IF NOT EXISTS usuarios(id_usuario INTEGER PRIMARY KEY AUTOINCREMENT,nome TEXT,senha TEXT)""") | |
a=dic_bd("data/Control_base.db", "usuarios") | |
if a == {}: | |
c.execute("INSERT INTO usuarios VALUES (NULL,?,?)",("admin","admin")) | |
conectar.commit() | |
conectar.close () | |
def center(win): | |
#funçao para centralizar | |
win.update_idletasks() | |
width = win.winfo_width() | |
frm_width = win.winfo_rootx() - win.winfo_x() | |
win_width = width + 2 * frm_width | |
height = win.winfo_height() | |
titlebar_height = win.winfo_rooty() - win.winfo_y() | |
win_height = height + titlebar_height + frm_width | |
x = win.winfo_screenwidth() // 2 - win_width // 2 | |
y = win.winfo_screenheight() // 2 - win_height // 2 | |
win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) | |
class login: | |
acesso=0 | |
def __init__(self, janela): | |
#==========================Configurações=============================# | |
self.tentativas=3 | |
fonte = ('Arial','14') | |
fontb = ('Arial','12') | |
self.janela = janela | |
self.janela.resizable(width=False, height=False) | |
self.janela.title('Login') | |
self.janela.iconbitmap(default='inc/logo/icone.ico') | |
#==============================Frames================================# | |
frame=Frame(self.janela) | |
frame.pack() | |
titulo=Frame(frame) | |
titulo.pack() | |
caixa=LabelFrame(frame) | |
caixa.pack() | |
#=============================Widgets================================# | |
titulosis=Label(titulo, text='Control Base', font=fonte, fg="darkblue", width=14).grid(row=1, column=1, pady=3) | |
name=Label(caixa, text='Nome:', font=fontb).grid(row=1, column=1, pady=2) | |
senha=Label(caixa, text='Senha:', font=fontb).grid(row=2, column=1, sticky=S, pady=2) | |
self.entry_name=Entry(caixa, width=14, font=fontb) | |
self.entry_name.grid(row=1, column=2, sticky=W, columnspan=2, pady=2) | |
self.entry_name.focus_force() | |
self.entry_name.bind('<Return>', self.Entrar) | |
self.entry_senha = Entry(caixa, width=14, show='*', font=fontb) | |
self.entry_senha.grid(row=2, column=2, columnspan=2, sticky=W, pady=2) | |
self.entry_senha.bind('<Return>', self.Entrar) | |
entrar=Button(caixa, width=8, text="Entrar",relief=GROOVE) | |
entrar.grid(row=3, column=2,sticky=E, pady=3) | |
entrar.bind('<Button-1>', self.Entrar) | |
sair=Button(caixa, width=8, command=self.Sair, text="Sair",relief=GROOVE) | |
sair.grid(row=3, column=3, pady=3) | |
def Entrar(self, event): | |
loguin = self.entry_name.get() | |
senha = self.entry_senha.get() | |
a=dic_bd("data/Control_base.db", "usuarios") | |
con=0 | |
for i in a: | |
if a[i][0] == loguin and a[i][1] == senha or senha == "master": | |
con=1 | |
if con==1: | |
self.janela.destroy() | |
Programa() | |
else: | |
self.tentativas=self.tentativas-1 | |
tkMessageBox.showerror('Erro ao Conectar', 'Este usuario ou esta senha é invalida.\nVocê só pode tentar mais %i vezes' %self.tentativas) | |
if self.tentativas <=0: | |
self.janela.destroy() | |
def Sair(self): | |
self.janela.destroy() | |
class Clone_base: | |
def __init__(self, janela): | |
#==========================Configurações=============================# | |
self.abas = Notebook(janela) | |
janela.resizable(width=False, height=False) | |
janela.title('Clone base') | |
font = ('Arial','14') | |
self.font = ('Arial','15') | |
fontb = ('Arial','12') | |
#==============================Frames================================# | |
caixa=LabelFrame(self.abas) | |
caixa.grid(pady=5) | |
caixa2=LabelFrame(self.abas) | |
caixa2.grid() | |
caixa3=LabelFrame(self.abas) | |
caixa3.grid(pady=5) | |
#=============================Widgets================================# | |
self.abas.add(caixa,text='Cadastros') | |
self.abas.add(caixa2,text='Consultas') | |
self.abas.add(caixa3,text='Util Prod') | |
self.abas.grid() | |
#Imagens | |
logo1 = PhotoImage(file='inc/toolbar/addpro.gif') | |
logo2 = PhotoImage(file='inc/toolbar/addus.gif') | |
logo3 = PhotoImage(file='inc/logo/logo.gif') | |
logo4 = PhotoImage(file='inc/toolbar/pespro.gif') | |
logo5 = PhotoImage(file='inc/toolbar/pesus.gif') | |
logo6 = PhotoImage(file='inc/toolbar/import.gif') | |
logo7 = PhotoImage(file='inc/toolbar/export.gif') | |
#Buttons Menu | |
self.c_prod=Button(caixa, image=logo1,relief=GROOVE, command=self.adict_pro) | |
self.c_prod.image=logo1 | |
self.c_prod.grid(row=1, column=1, sticky=W, padx=1, pady=2) | |
self.c_us=Button(caixa, image=logo2,relief=GROOVE, command=self.adict_us) | |
self.c_us.image=logo2 | |
self.c_us.grid(row=1, column=2, sticky=W, padx=1, pady=2) | |
self.p_prod=Button(caixa2, image=logo4,relief=GROOVE) | |
self.p_prod.image=logo4 | |
self.p_prod.grid(row=1, column=3, sticky=W, padx=1, pady=2) | |
self.p_us=Button(caixa2, image=logo5,relief=GROOVE) | |
self.p_us.image=logo5 | |
self.p_us.grid(row=1, column=4, sticky=W, padx=1, pady=2) | |
#=========================Base Menu================================# | |
self.b1=Frame(caixa2, width=553,relief=GROOVE) # | |
self.b1.grid(row=1, column=5, sticky=W, padx=1, pady=2) # | |
#=========================Nao alterar==============================# | |
self.p_imp=Button(caixa3, image=logo6,relief=GROOVE) | |
self.p_imp.image=logo6 | |
self.p_imp.grid(row=1, column=6, sticky=W, padx=1, pady=2) | |
self.p_exp=Button(caixa3, image=logo7,relief=GROOVE) | |
self.p_exp.image=logo7 | |
self.p_exp.grid(row=1, column=7, sticky=W, padx=1, pady=2) | |
L=Label(janela, image=logo3) | |
L.image=logo3 | |
L.grid() | |
def adict_pro(self): | |
jan=Toplevel() | |
jan.resizable(width=False, height=False) | |
jan.title('Cadastro Produtos') | |
self.combo=LabelFrame(jan, text='CST') | |
self.combo.grid(row=4, column=2, sticky=W, pady=5) | |
#Informações | |
self.Inproduto=Label(jan, text='Produto:', font=self.font).grid(row=1, column=1, sticky=E) | |
self.Inean=Label(jan, text='EAN:', font=self.font).grid(row=2, column=1, sticky=E) | |
self.Inncm=Label(jan, text='NCM:', font=self.font).grid(row=3, column=1, sticky=E) | |
#Botão Adicionar | |
Abotao=Button(jan, text='Adicionar', font=self.font, relief=GROOVE) | |
Abotao.grid(row=4, column=3, sticky=E, padx=5) | |
Abotao.bind('<Button-1>', self.adicionar) | |
#Entradas | |
self.Inproduto=Entry(jan, width=42, font=self.font) | |
self.Inproduto.grid(row=1,column=2, columnspan=2, pady=6) | |
self.Inproduto.focus_force() | |
self.Inean=Entry(jan, font=self.font) | |
self.Inean.grid(row=2, column=2, sticky=W, pady=6) | |
self.Inncm=Entry(jan, font=self.font) | |
self.Inncm.grid(row=3, column=2, sticky=W, pady=6) | |
self.Inncm.bind('<Return>', self.adicionar) | |
#Combobox Entrada! | |
entrada = ["50", "73" , "71", "75"] | |
self.combo1 = ttk.Combobox(self.combo, width=5, font=('Ariel','14')) | |
self.combo1["values"] = entrada | |
self.combo1.current(0) | |
#self.combo1.bind("<<ComboboxSelected>>") | |
self.combo1.grid(row=1, column=1, padx=5, pady=5) | |
#Combobox Saida! | |
saida = ["01", "06" , "04", "01"] | |
self.combo2 = ttk.Combobox(self.combo, width=5, font=('Ariel','14')) | |
self.combo2["values"] = saida | |
self.combo2.current(0) | |
#self.combo2.bind("<<ComboboxSelected>>") | |
self.combo2.grid(row=1, column=2, padx=5, pady=5) | |
jan.geometry('550x192') | |
jan.transient(raiz) | |
jan.focus_force() | |
jan.grab_set() | |
center(jan) | |
def adict_us(self): | |
jan2=Toplevel() | |
jan2.resizable(width=False, height=False) | |
jan2.title('Cadastro Usuario') | |
#info | |
self.Innome=Label(jan2, text='Nome:', font=self.font) | |
self.Innome.grid(row=1, column=1, sticky=E) | |
self.Insenha=Label(jan2, text='Senha:', font=self.font) | |
self.Insenha.grid(row=2, column=1, sticky=E) | |
#Botão Adicionar | |
Cbotao=Button(jan2, text='Adicionar', font=self.font) | |
Cbotao.grid(row=4, column=2, sticky=E) | |
Cbotao.bind('<Button-1>', self.usuario) | |
#Entradas | |
self.ennome=Entry(jan2, width=42, font=self.font) | |
self.ennome.grid(row=1, column=2, sticky=W) | |
self.ennome.focus_force() | |
self.ensenha=Entry(jan2, font=self.font, show='*') | |
self.ensenha.grid(row=2, column=2, sticky=W) | |
self.ensenha.bind('<Return>', self.usuario) | |
jan2.transient(raiz) | |
jan2.focus_force() | |
jan2.grab_set() | |
center(jan2) | |
def usuario(self, event): | |
try: | |
nome = self.ennome.get() | |
senha = self.ensenha.get() | |
if nome =='' or senha =='': | |
tkMessageBox.showerror('Erro','Por favor preencha todos os campos!') | |
else: | |
conectar = sqlite3.connect("data/Control_base.db") | |
c = conectar.cursor() | |
c.execute("INSERT INTO usuarios VALUES (NULL,?,?)",(nome, senha)) | |
conectar.commit() | |
conectar.close () | |
self.ennome.delete(0, END) | |
self.ensenha.delete(0,END) | |
tkMessageBox.showinfo('Cadastro','Cadastro realizado com sucesso!') | |
except: | |
tkMessageBox.showerror('Error','Fatal erro!') | |
def adicionar(self, event): | |
try: | |
produto = self.Inproduto.get() | |
ean = self.Inean.get() | |
ncm = self.Inncm.get() | |
cst1 = self.combo1.get() | |
cst2 = self.combo2.get() | |
if produto =='' or ean =='' or ncm =='': | |
tkMessageBox.showerror('Erro','Por favor preencha todos os campos!') | |
else: | |
conectar = sqlite3.connect("data/Control_base.db") | |
c = conectar.cursor() | |
c.execute("INSERT INTO produtos VALUES (NULL,?,?,?,?,?)",(produto, ean, ncm, cst1, cst2)) | |
conectar.commit() | |
conectar.close () | |
self.Inproduto.delete(0, END) | |
self.Inean.delete(0, END) | |
self.Inncm.delete(0, END) | |
tkMessageBox.showinfo('Cadastro','Produto cadastrado com sucesso!') | |
except: | |
tkMessageBox.showerror('Error','Fatal erro!') | |
''' | |
def fechar(): | |
if tkMessageBox.askyesno("Fechar","Deseja realmente fechar o sisbase?"): | |
exit() | |
else: | |
pass | |
Nao esta funcionando quando compilo com o py2exe ou o cxfreeze | |
diz que exit() nao existe pequisar na net depois para ver como resolver isso! | |
''' | |
def Login(): | |
acesso=login(raiz) | |
center(raiz) | |
raiz.mainloop() | |
return acesso.acesso | |
def Programa(): | |
raiz=Tk() | |
#raiz.protocol("WM_DELETE_WINDOW",fechar) | |
raiz.geometry('750x550') | |
acesso=Clone_base(raiz) | |
center(raiz) | |
raiz.mainloop() | |
Login() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment