Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: utf-8
- import MySQLdb
- from Tkinter import *
- from maskedentry import MaskedWidget
- import sys
- import tkMessageBox
- global dados
- import ttk
- #lista = ["willian","selma","jose","carlos"]
- lista =[]
- novofuncionario = True
- class CadastroFuncionario():
- def __init__(self,master):
- self.master = master
- self.master.title("Cadastro de Funcionario")
- self.master.geometry("900x600")
- Label(self.master,text="Pesquisar:").place(relx=0.5,x=-175,y=30,anchor=CENTER)
- self.PesquisaFuncionario = Entry(self.master,)
- self.PesquisaFuncionario.place(relx=0.5,x=-50,y=30,anchor=CENTER,height=17,width=200)
- Label(self.master,text="Código:").place(relx=0.5,x=-175,y=60,anchor=CENTER)
- self.CodigoFuncionario = Entry(self.master,state="readonly")
- self.CodigoFuncionario.place(relx=0.5,x=-140,y=60,anchor=CENTER,height=17,width=30)
- Label(self.master,text="Nome:").place(relx=0.5,x=-175,y=90,anchor=CENTER)
- self.NomeFuncionario = Entry(self.master,)
- self.NomeFuncionario.place(relx=0.5,x=-30,y=90,anchor=CENTER,height=17,width=250)
- Label(self.master,text="Endereco:").place(relx=0.5,x=-183,y=120,anchor=CENTER)
- self.EnderecoFuncionario = Entry(self.master)
- self.EnderecoFuncionario.place(relx=0.5,x=-43,y=120,anchor=CENTER,height=17,width=220)
- Label(self.master,text="Bairro:").place(relx=0.5,x=-175,y=150,anchor=CENTER)
- self.BairroFuncionario = Entry(self.master)
- self.BairroFuncionario.place(relx=0.5,x=-80,y=150,anchor=CENTER,height=17,width=150)
- Label(self.master,text="Cidade:").place(relx=0.5,x=20,y=150,anchor=CENTER)
- self.CidadeFuncionario = Entry(self.master)
- self.CidadeFuncionario.place(relx=0.5,x=112,y=150,anchor=CENTER,height=17,width=140)
- Label(self.master,text="CEP:").place(relx=0.5,x=43,y=180,anchor=CENTER)
- self.cep = Entry(self.master)
- #self.cep = MaskedWidget(self.master,'fixed',mask="99999 999")
- self.cep.place(relx=0.5,x=100,y=180,anchor=CENTER,height=17,width=100)
- Label(self.master,text="Estado:").place(relx=0.5,x=-177,y=180,anchor=CENTER)
- self.comboEstado = ttk.Combobox(self.master)
- self.comboEstado["values"] = 'AC','AL','AP','AM','BA','CE','DF','ES','GO','MA','MT','MS','MG','PA','PB','PR','PE','PI','RJ','RN','RS','RO','RR','SC','SP','SE','TO'
- self.comboEstado.current(0)
- self.comboEstado.place(relx=0.5,x=-100,y=180,anchor=CENTER,height=17,width=100)
- """
- self.var_estado = StringVar(self.master)
- self.var_estado.set('AC')
- self.estado = OptionMenu(self.master,self.var_estado,'AC','AL','AP','AM','BA','CE','DF','ES','GO','MA','MT','MS','MG','PA','PB','PR','PE','PI','RJ','RN','RS','RO','RR','SC','SP','SE','TO')
- #self.EstadoFuncionario = Entry(self.master)
- self.estado.config(width=15)
- self.estado.place(relx=0.5,x=-100,y=150,anchor=CENTER,height=17,width=100)
- #self.EstadoFuncionario.place(relx=0.5,x=-65,y=120,anchor=CENTER,height=17,width=180)
- """
- Label(self.master,text="Salario:").place(relx=0.5,x=-177,y=210,anchor=CENTER)
- self.salario = Entry(self.master)
- self.salario.place(relx=0.5,x=-96,y=210,anchor=CENTER,height=17,width=120)
- Label(self.master,text="Telefone Residencial:").place(relx=0.5,x=-142,y=240,anchor=CENTER)
- self.TelefoneResidencial = Entry(self.master)
- #self.TelefoneResidencial = MaskedWidget(self.master,'fixed',mask="99 99999 9999")
- self.TelefoneResidencial.place(relx=0.5,x=-35,y=240,anchor=CENTER,height=17,width=100)
- Label(self.master,text="Telefone Celular:").place(relx=0.5,x=-152,y=270,anchor=CENTER)
- self.TelefoneCelular = Entry(self.master)
- #self.TelefoneCelular = MaskedWidget(self.master,'fixed',mask="99 99999 9999")
- self.TelefoneCelular.place(relx=0.5,x=-40,y=270,anchor=CENTER,height=17,width=100)
- Label(self.master,text="Email:").place(relx=0.5,x=-172,y=300,anchor=CENTER)
- self.EmailFuncionario = Entry(self.master)
- self.EmailFuncionario.place(relx=0.5,x=-43,y=300,anchor=CENTER,height=17,width=220)
- BANCO ="tcc teste"
- USER="root"
- PASSWD=""
- HOST="127.0.0.1"
- conexao = MySQLdb.connect(db=BANCO,user=USER,passwd=PASSWD,host=HOST)
- cursor = conexao.cursor()
- sql = "select login from usuarios where tipo_usuario like 'empregada' "
- cursor.execute(sql)
- dadosFuncionario = cursor.fetchall()
- for dados in dadosFuncionario:
- lista.append(dados)
- #contator = len(lista)
- #print len(lista)
- Label(self.master,text="Login:").place(relx=0.5,x=-172,y=330,anchor=CENTER)
- self.comboLogin = ttk.Combobox(self.master)
- self.comboLogin["values"] = lista
- self.comboLogin.current(0)
- self.comboLogin.place(relx=0.5,x=-100,y=330,anchor=CENTER,height=17,width=100)
- """
- self.var_us = StringVar(self.master)
- self.var_us.set('')
- self.nivelus = OptionMenu(self.master, 'teste','teste')
- self.nivelus.config(width=15)
- self.nivelus.place(relx=0.5,x=-100,y=300,anchor=CENTER,height=17,width=100)
- """
- self.FuncionarioNovo = Button(self.master,width=10, text="Novo",command=self.novo)
- self.FuncionarioNovo.place(relx=0.5,x=-150,y=370,anchor=CENTER,height=25,width=90)
- #self.BotaoPesquisa = Button(self.master,width=10, text="Pesquisar")
- #self.BotaoPesquisa.grid(row=1,column=3)
- self.GravarFuncionario = Button(self.master,width=10, text="Gravar",command=self.CadastroFuncionario)
- self.GravarFuncionario.place(relx=0.5,x=-60,y=370,anchor=CENTER,height=25,width=90)
- self.FuncionarioAlterar = Button(self.master,width=10, text="Alterar", command=self.alterar)
- self.FuncionarioAlterar.place(relx=0.5,x=30,y=370,anchor=CENTER,height=25,width=90)
- self.FuncionarioExcluir = Button(self.master,width=10, text="Excluir", command=self.excluir)
- self.FuncionarioExcluir.place(relx=0.5,x=120,y=370,anchor=CENTER,height=25,width=90)
- self.FuncionarioCancelar = Button(self.master,width=10, text="Cancelar",command=self.cancelar)
- self.FuncionarioCancelar.place(relx=0.5,x=210,y=370,anchor=CENTER,height=25,width=90)
- self.PesquisarFuncionario = Button(self.master,width=10, text="Pesquisar", command=self.pesquisar)
- self.PesquisarFuncionario.place(relx=0.5,x=100,y=30,anchor=CENTER,height=25,width=90)
- def habilitarcampos(self):
- self.NomeFuncionario.configure(state="normal")
- self.EnderecoFuncionario.configure(state="normal")
- self.BairroFuncionario.configure(state="normal")
- self.CidadeFuncionario.configure(state="normal")
- self.cep.configure(state="normal")
- self.salario.configure(state="normal")
- self.TelefoneResidencial.configure(state="normal")
- self.TelefoneCelular.configure(state="normal")
- self.EmailFuncionario.configure(state="normal")
- def limpacampos(self):
- #editcodigo.configure(state="normal") # Habilita o campo codigo se precisa
- self.CodigoFuncionario.configure(state="normal")
- self.CodigoFuncionario.delete(0,END)
- self.NomeFuncionario.delete(0,END)
- self.EnderecoFuncionario.delete(0,END)
- self.BairroFuncionario.delete(0,END)
- self.CidadeFuncionario.delete(0,END)
- self.cep.delete(0,END)
- self.salario.delete(0,END)
- self.TelefoneResidencial.delete(0,END)
- self.TelefoneCelular.delete(0,END)
- self.EmailFuncionario.delete(0,END)
- self.NomeFuncionario.focus()
- def novo(self):
- self.habilitarcampos()
- self.limpacampos()
- self.CodigoFuncionario.configure(state="readonly")
- self.FuncionarioExcluir.configure(state="disabled")
- self.FuncionarioAlterar.configure(state="disabled")
- self.FuncionarioCancelar.configure(state="normal")
- self.GravarFuncionario.configure(state="normal")
- self.comboLogin.configure(state="normal")
- self.comboEstado.configure(state="normal")
- #self.StatusResposta['text']=" "
- #self.StatusResposta['text']="Inserindo Novo Registro"
- def desabilitarcampos(self):
- #editcodigo.configure(state="readonly")
- self.CodigoFuncionario.configure(state="readonly")
- self.NomeFuncionario.configure(state="readonly")
- self.EnderecoFuncionario.configure(state="readonly")
- self.BairroFuncionario.configure(state="readonly")
- self.CidadeFuncionario.configure(state="readonly")
- self.cep.configure(state="readonly")
- self.salario.configure(state="readonly")
- self.TelefoneResidencial.configure(state="readonly")
- self.TelefoneCelular.configure(state="readonly")
- self.EmailFuncionario.configure(state="readonly")
- self.comboEstado.configure(state="disabled")
- self.comboLogin.configure(state="disabled")
- def cancelar(self):
- self.desabilitarcampos()
- self.FuncionarioExcluir.configure(state="disabled")
- self.FuncionarioAlterar.configure(state="disabled")
- self.FuncionarioNovo.configure(state="normal")
- self.GravarFuncionario.configure(state="disabled")
- self.FuncionarioCancelar.configure(state="disabled")
- def CadastroFuncionario(self):
- conexao = self.ConexaoBanco()
- codigo=self.CodigoFuncionario.get()
- nome=self.NomeFuncionario.get()
- endereco=self.EnderecoFuncionario.get()
- bairro=self.BairroFuncionario.get()
- cidade=self.CidadeFuncionario.get()
- cep=self.cep.get()
- estado=self.comboEstado.get()
- salario=self.salario.get()
- telefonecasa=self.TelefoneResidencial.get()
- celular=self.TelefoneCelular.get()
- email=self.EmailFuncionario.get()
- login=self.comboLogin.get()
- cursor = conexao.cursor()
- """
- cursor = conexao.cursor()
- sql = "select * from funcionarios where nome like '"+nome+"'"
- cursor.execute(sql)
- valido = cursor.fetchall()
- if len(valido)>0:
- tkMessageBox.showinfo("Alerta", "Empregado já cadastrado")
- """
- if self.NomeFuncionario.get() == "":
- tkMessageBox.showinfo("Alerta", "Preencha o Campo nome")
- elif self.EnderecoFuncionario.get() == "":
- tkMessageBox.showinfo("Alerta", "Preencha o Campo Endereco")
- elif self.BairroFuncionario.get() == "":
- tkMessageBox.showinfo("Alerta", "Preencha o Campo Bairro")
- elif self.CidadeFuncionario.get() == "":
- tkMessageBox.showinfo("Alerta", "Preencha o Campo Cidade")
- elif self.cep.get() == "":
- tkMessageBox.showinfo("Alerta", "Preencha o Campo CEP")
- elif self.salario.get() == "":
- tkMessageBox.showinfo("Alerta", "Preencha o Campo Salario")
- elif self.TelefoneResidencial.get()== "" and self.TelefoneCelular.get() == "":
- tkMessageBox.showinfo("Alerta", "É necessário fornecer pelo menos 1 número de telefone")
- elif novofuncionario:
- sql = "insert into funcionarios (nome,endereco,cep,salario,email,estado,cidade,bairro,telefone_residencial,telefone_celular,login_usuario) values ('"+nome+"','"+endereco+"','"+cep+"','"+salario+"','"+email+"','"+estado+"','"+cidade+"','"+bairro+"','"+telefonecasa+"','"+celular+"','"+login+"')"
- else:
- sql = "update funcionarios set nome = '"+self.NomeFuncionario.get()+"',endereco = '"+self.EnderecoFuncionario.get()+"',cep = '"+self.cep.get()+"',salario = '"+self.salario.get()+"',email = '"+self.EmailFuncionario.get()+"',cidade = '"+self.CidadeFuncionario.get()+"',bairro = '"+self.BairroFuncionario.get()+"',telefone_residencial = '"+self.TelefoneResidencial.get()+"',telefone_celular = '"+self.TelefoneCelular.get()+"' where id_funcionario = "+codigo
- try:
- cursor.execute(sql)
- conexao.commit()
- tkMessageBox.showinfo("Alerta", nome + "Cadastrodo Com sucesso")
- except MySQLdb as erro:
- print("Não COnsegui gravar os arquivos no banco de dados", erro)
- self.FuncionarioExcluir.configure(state="normal")
- self.FuncionarioAlterar.configure(state="normal")
- self.FuncionarioNovo.configure(state="normal")
- self.GravarFuncionario.configure(state="disabled")
- self.FuncionarioCancelar.configure(state="disabled")
- self.desabilitarcampos()
- conexao.close()
- def alterar(self):
- self.habilitarcampos()
- self.FuncionarioExcluir.configure(state="disabled")
- self.FuncionarioAlterar.configure(state="disabled")
- self.FuncionarioNovo.configure(state="disabled")
- self.GravarFuncionario.configure(state="normal")
- self.FuncionarioCancelar.configure(state="normal")
- self.comboEstado.configure(state="disabled")
- global novofuncionario
- novofuncionario = False
- def pesquisar(self):
- self.FuncionarioExcluir.configure(state="normal")
- self.FuncionarioAlterar.configure(state="normal")
- conexao = self.ConexaoBanco()
- cursor = conexao.cursor()
- if self.PesquisaFuncionario.get() == "":
- tkMessageBox.showinfo("Alerta", "Digite um Funcionario a ser pesquisado")
- sql = "select * from funcionarios where nome like '"+self.PesquisaFuncionario.get()+"%'"
- try:
- cursor.execute(sql)
- dadosFuncionario = cursor.fetchone()
- self.limpacampos()
- if dadosFuncionario:
- #print(dadosPessoa[0])
- self.CodigoFuncionario.insert(0,dadosFuncionario[0])
- self.CodigoFuncionario.configure(state="readonly")
- #self.comboLogin.configure(state="disabled")
- self.comboEstado.configure(state="disabled")
- self.NomeFuncionario.insert(0,dadosFuncionario[1])
- self.EnderecoFuncionario.insert(0,dadosFuncionario[2])
- self.cep.insert(0,dadosFuncionario[3])
- self.salario.insert(0,dadosFuncionario[4])
- self.EmailFuncionario.insert(0,dadosFuncionario[5])
- #self.comboEstado.insert(0,dadosFuncionario[5])
- self.CidadeFuncionario.insert(0,dadosFuncionario[7])
- self.BairroFuncionario.insert(0,dadosFuncionario[8])
- self.TelefoneResidencial.insert(0,dadosFuncionario[9])
- self.TelefoneCelular.insert(0,dadosFuncionario[10])
- self.comboLogin.insert(0,dadosFuncionario[11])
- else:
- tkMessageBox.showinfo("Alerta", "Funcionario Não cadastrado")
- except MySQLdb as erro:
- print("Não conseguiu listar ",erro)
- conexao.close()
- def excluir(self):
- conexao = self.ConexaoBanco()
- #codigo= int (raw_input("Codigo"))
- ExcluirFuncionario = self.PesquisaFuncionario.get()
- codigofuncionario = self.CodigoFuncionario.get()
- cursor = conexao.cursor()
- sql = "delete from funcionarios where id_funcionario = "+codigofuncionario
- try:
- cursor.execute(sql)
- conexao.commit()
- tkMessageBox.showinfo("Alerta", "Dados Excluidos Com sucesso")
- #print("Dados Excluidos com sucesso")
- except MySQLdb as erro:
- tkMessageBox.showerro("Alerta", "Não Foi Possivel Excluir os Dados")
- #print("Não COnsegui Excluir os arquivos no banco de dados", erro)
- self.limpacampos()
- conexao.close()
- def ConexaoBanco(self):
- BANCO ="tcc teste"
- USER="root"
- PASSWD=""
- HOST="127.0.0.1"
- try:
- conexao = MySQLdb.connect(db=BANCO,user=USER,passwd=PASSWD,host=HOST)
- except:
- print("Conexão com o banco de dados falhou")
- return conexao
- def pesquisarLogin(self):
- conexao = self.ConexaoBanco()
- cursor = conexao.cursor()
- sql = "select login from usuarios "
- #if self.EditUsuario.get() == "":
- # tkMessageBox.showinfo("Alerta", "Digite um Usuario A ser Pesquisado")
- try:
- cursor.execute(sql)
- dadosFuncionario = cursor.fetchall()
- for dados in dadosFuncionario:
- self.listalogin["values"] = dados
- except MySQLdb as erro:
- print("Não Consegui Listar:",erro)
- conexao.close()
- print self.listalogin
- def TelaCadastroFuncionario():
- root=Tk()
- CadastroFuncionario(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement