Advertisement
willianboveloni

Como Eliminar lixo em Arquivos em uma Lista

Nov 5th, 2015
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.32 KB | None | 0 0
  1. # coding: utf-8
  2. import MySQLdb
  3. from Tkinter import *
  4. from maskedentry import MaskedWidget
  5. import sys
  6. import tkMessageBox
  7. global dados
  8. import ttk
  9. #lista = ["willian","selma","jose","carlos"]
  10. lista =[]
  11. novofuncionario = True
  12.  
  13.  
  14.  
  15.  
  16. class CadastroFuncionario():
  17.  
  18.     def __init__(self,master):
  19.         self.master = master
  20.         self.master.title("Cadastro de Funcionario")
  21.         self.master.geometry("900x600")
  22.  
  23.         Label(self.master,text="Pesquisar:").place(relx=0.5,x=-175,y=30,anchor=CENTER)
  24.         self.PesquisaFuncionario = Entry(self.master,)
  25.         self.PesquisaFuncionario.place(relx=0.5,x=-50,y=30,anchor=CENTER,height=17,width=200)
  26.  
  27.         Label(self.master,text="Código:").place(relx=0.5,x=-175,y=60,anchor=CENTER)
  28.         self.CodigoFuncionario = Entry(self.master,state="readonly")
  29.         self.CodigoFuncionario.place(relx=0.5,x=-140,y=60,anchor=CENTER,height=17,width=30)
  30.  
  31.         Label(self.master,text="Nome:").place(relx=0.5,x=-175,y=90,anchor=CENTER)
  32.         self.NomeFuncionario = Entry(self.master,)
  33.         self.NomeFuncionario.place(relx=0.5,x=-30,y=90,anchor=CENTER,height=17,width=250)
  34.        
  35.        
  36.        
  37.         Label(self.master,text="Endereco:").place(relx=0.5,x=-183,y=120,anchor=CENTER)
  38.         self.EnderecoFuncionario = Entry(self.master)
  39.         self.EnderecoFuncionario.place(relx=0.5,x=-43,y=120,anchor=CENTER,height=17,width=220)
  40.        
  41.        
  42.         Label(self.master,text="Bairro:").place(relx=0.5,x=-175,y=150,anchor=CENTER)
  43.         self.BairroFuncionario = Entry(self.master)
  44.         self.BairroFuncionario.place(relx=0.5,x=-80,y=150,anchor=CENTER,height=17,width=150)
  45.        
  46.         Label(self.master,text="Cidade:").place(relx=0.5,x=20,y=150,anchor=CENTER)
  47.         self.CidadeFuncionario = Entry(self.master)
  48.         self.CidadeFuncionario.place(relx=0.5,x=112,y=150,anchor=CENTER,height=17,width=140)
  49.        
  50.         Label(self.master,text="CEP:").place(relx=0.5,x=43,y=180,anchor=CENTER)
  51.         self.cep = Entry(self.master)
  52.         #self.cep = MaskedWidget(self.master,'fixed',mask="99999 999")
  53.         self.cep.place(relx=0.5,x=100,y=180,anchor=CENTER,height=17,width=100)
  54.        
  55.        
  56.         Label(self.master,text="Estado:").place(relx=0.5,x=-177,y=180,anchor=CENTER)
  57.        
  58.         self.comboEstado = ttk.Combobox(self.master)
  59.         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'
  60.         self.comboEstado.current(0)
  61.         self.comboEstado.place(relx=0.5,x=-100,y=180,anchor=CENTER,height=17,width=100)
  62.  
  63.         """
  64.        self.var_estado = StringVar(self.master)
  65.        self.var_estado.set('AC')
  66.        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')
  67.        #self.EstadoFuncionario = Entry(self.master)
  68.        self.estado.config(width=15)
  69.        self.estado.place(relx=0.5,x=-100,y=150,anchor=CENTER,height=17,width=100)
  70.  
  71.        #self.EstadoFuncionario.place(relx=0.5,x=-65,y=120,anchor=CENTER,height=17,width=180)
  72.        """
  73.         Label(self.master,text="Salario:").place(relx=0.5,x=-177,y=210,anchor=CENTER)
  74.         self.salario = Entry(self.master)
  75.         self.salario.place(relx=0.5,x=-96,y=210,anchor=CENTER,height=17,width=120)
  76.        
  77.         Label(self.master,text="Telefone Residencial:").place(relx=0.5,x=-142,y=240,anchor=CENTER)
  78.         self.TelefoneResidencial = Entry(self.master)
  79.         #self.TelefoneResidencial = MaskedWidget(self.master,'fixed',mask="99 99999 9999")
  80.         self.TelefoneResidencial.place(relx=0.5,x=-35,y=240,anchor=CENTER,height=17,width=100)
  81.        
  82.        
  83.         Label(self.master,text="Telefone Celular:").place(relx=0.5,x=-152,y=270,anchor=CENTER)
  84.         self.TelefoneCelular = Entry(self.master)
  85.         #self.TelefoneCelular = MaskedWidget(self.master,'fixed',mask="99 99999 9999")
  86.         self.TelefoneCelular.place(relx=0.5,x=-40,y=270,anchor=CENTER,height=17,width=100)
  87.        
  88.        
  89.        
  90.        
  91.        
  92.         Label(self.master,text="Email:").place(relx=0.5,x=-172,y=300,anchor=CENTER)
  93.         self.EmailFuncionario = Entry(self.master)
  94.         self.EmailFuncionario.place(relx=0.5,x=-43,y=300,anchor=CENTER,height=17,width=220)
  95.  
  96.         BANCO ="tcc teste"
  97.         USER="root"
  98.         PASSWD=""
  99.         HOST="127.0.0.1"
  100.        
  101.         conexao = MySQLdb.connect(db=BANCO,user=USER,passwd=PASSWD,host=HOST)
  102.  
  103.         cursor = conexao.cursor()
  104.         sql = "select login from usuarios where tipo_usuario like 'empregada' "
  105.  
  106.         cursor.execute(sql)
  107.          
  108.         dadosFuncionario = cursor.fetchall()
  109.  
  110.         for dados in dadosFuncionario:
  111.                 lista.append(dados)
  112.            
  113.  
  114.         #contator  = len(lista)
  115.         #print len(lista)
  116.        
  117.         Label(self.master,text="Login:").place(relx=0.5,x=-172,y=330,anchor=CENTER)
  118.  
  119.         self.comboLogin = ttk.Combobox(self.master)
  120.         self.comboLogin["values"] = lista
  121.         self.comboLogin.current(0)
  122.         self.comboLogin.place(relx=0.5,x=-100,y=330,anchor=CENTER,height=17,width=100)
  123.  
  124.  
  125.          
  126.        
  127.        
  128.         """
  129.        self.var_us = StringVar(self.master)
  130.        self.var_us.set('')
  131.        self.nivelus = OptionMenu(self.master, 'teste','teste')
  132.      
  133.        self.nivelus.config(width=15)
  134.        self.nivelus.place(relx=0.5,x=-100,y=300,anchor=CENTER,height=17,width=100)
  135.        """
  136.        
  137.  
  138.         self.FuncionarioNovo = Button(self.master,width=10, text="Novo",command=self.novo)
  139.         self.FuncionarioNovo.place(relx=0.5,x=-150,y=370,anchor=CENTER,height=25,width=90)
  140.  
  141.         #self.BotaoPesquisa = Button(self.master,width=10, text="Pesquisar")
  142.         #self.BotaoPesquisa.grid(row=1,column=3)
  143.  
  144.         self.GravarFuncionario = Button(self.master,width=10, text="Gravar",command=self.CadastroFuncionario)
  145.         self.GravarFuncionario.place(relx=0.5,x=-60,y=370,anchor=CENTER,height=25,width=90)
  146.  
  147.         self.FuncionarioAlterar = Button(self.master,width=10, text="Alterar", command=self.alterar)
  148.         self.FuncionarioAlterar.place(relx=0.5,x=30,y=370,anchor=CENTER,height=25,width=90)
  149.  
  150.         self.FuncionarioExcluir = Button(self.master,width=10, text="Excluir", command=self.excluir)
  151.         self.FuncionarioExcluir.place(relx=0.5,x=120,y=370,anchor=CENTER,height=25,width=90)
  152.  
  153.         self.FuncionarioCancelar = Button(self.master,width=10, text="Cancelar",command=self.cancelar)
  154.         self.FuncionarioCancelar.place(relx=0.5,x=210,y=370,anchor=CENTER,height=25,width=90)
  155.  
  156.         self.PesquisarFuncionario = Button(self.master,width=10, text="Pesquisar", command=self.pesquisar)
  157.         self.PesquisarFuncionario.place(relx=0.5,x=100,y=30,anchor=CENTER,height=25,width=90)
  158.  
  159.  
  160.     def habilitarcampos(self):
  161.         self.NomeFuncionario.configure(state="normal")
  162.         self.EnderecoFuncionario.configure(state="normal")
  163.         self.BairroFuncionario.configure(state="normal")
  164.         self.CidadeFuncionario.configure(state="normal")
  165.         self.cep.configure(state="normal")
  166.         self.salario.configure(state="normal")
  167.         self.TelefoneResidencial.configure(state="normal")
  168.         self.TelefoneCelular.configure(state="normal")
  169.         self.EmailFuncionario.configure(state="normal")
  170.  
  171.  
  172.     def limpacampos(self):
  173.         #editcodigo.configure(state="normal") # Habilita o campo codigo se precisa
  174.         self.CodigoFuncionario.configure(state="normal")
  175.         self.CodigoFuncionario.delete(0,END)
  176.    
  177.         self.NomeFuncionario.delete(0,END)
  178.         self.EnderecoFuncionario.delete(0,END)
  179.         self.BairroFuncionario.delete(0,END)
  180.         self.CidadeFuncionario.delete(0,END)
  181.         self.cep.delete(0,END)
  182.         self.salario.delete(0,END)
  183.         self.TelefoneResidencial.delete(0,END)
  184.         self.TelefoneCelular.delete(0,END)
  185.         self.EmailFuncionario.delete(0,END)
  186.         self.NomeFuncionario.focus()
  187.  
  188.  
  189.     def novo(self):
  190.         self.habilitarcampos()
  191.         self.limpacampos()
  192.         self.CodigoFuncionario.configure(state="readonly")
  193.         self.FuncionarioExcluir.configure(state="disabled")
  194.         self.FuncionarioAlterar.configure(state="disabled")
  195.         self.FuncionarioCancelar.configure(state="normal")
  196.         self.GravarFuncionario.configure(state="normal")
  197.         self.comboLogin.configure(state="normal")
  198.         self.comboEstado.configure(state="normal")
  199.          #self.StatusResposta['text']=" "
  200.          #self.StatusResposta['text']="Inserindo Novo Registro"
  201.  
  202.     def desabilitarcampos(self):
  203.         #editcodigo.configure(state="readonly")
  204.         self.CodigoFuncionario.configure(state="readonly")
  205.         self.NomeFuncionario.configure(state="readonly")
  206.         self.EnderecoFuncionario.configure(state="readonly")
  207.         self.BairroFuncionario.configure(state="readonly")
  208.         self.CidadeFuncionario.configure(state="readonly")
  209.         self.cep.configure(state="readonly")
  210.         self.salario.configure(state="readonly")
  211.         self.TelefoneResidencial.configure(state="readonly")
  212.         self.TelefoneCelular.configure(state="readonly")
  213.         self.EmailFuncionario.configure(state="readonly")
  214.         self.comboEstado.configure(state="disabled")
  215.         self.comboLogin.configure(state="disabled")
  216.  
  217.     def cancelar(self):
  218.         self.desabilitarcampos()
  219.         self.FuncionarioExcluir.configure(state="disabled")
  220.         self.FuncionarioAlterar.configure(state="disabled")
  221.         self.FuncionarioNovo.configure(state="normal")
  222.         self.GravarFuncionario.configure(state="disabled")
  223.    
  224.         self.FuncionarioCancelar.configure(state="disabled")
  225.  
  226.  
  227.     def CadastroFuncionario(self):
  228.        
  229.         conexao = self.ConexaoBanco()
  230.        
  231.        
  232.         codigo=self.CodigoFuncionario.get()
  233.         nome=self.NomeFuncionario.get()
  234.         endereco=self.EnderecoFuncionario.get()
  235.         bairro=self.BairroFuncionario.get()
  236.         cidade=self.CidadeFuncionario.get()
  237.         cep=self.cep.get()
  238.         estado=self.comboEstado.get()
  239.         salario=self.salario.get()
  240.         telefonecasa=self.TelefoneResidencial.get()
  241.         celular=self.TelefoneCelular.get()
  242.         email=self.EmailFuncionario.get()
  243.         login=self.comboLogin.get()
  244.  
  245.         cursor = conexao.cursor()
  246.         """
  247.        cursor = conexao.cursor()
  248.  
  249.        sql = "select * from  funcionarios where nome like '"+nome+"'"
  250.        cursor.execute(sql)
  251.        valido = cursor.fetchall()
  252.  
  253.        if len(valido)>0:
  254.            tkMessageBox.showinfo("Alerta", "Empregado já cadastrado")
  255.        """
  256.         if self.NomeFuncionario.get() == "":
  257.             tkMessageBox.showinfo("Alerta", "Preencha o Campo nome")
  258.  
  259.         elif self.EnderecoFuncionario.get() == "":
  260.             tkMessageBox.showinfo("Alerta", "Preencha o Campo Endereco")
  261.  
  262.         elif self.BairroFuncionario.get() == "":
  263.             tkMessageBox.showinfo("Alerta", "Preencha o Campo Bairro")
  264.            
  265.         elif self.CidadeFuncionario.get() == "":
  266.             tkMessageBox.showinfo("Alerta", "Preencha o Campo Cidade")
  267.  
  268.         elif self.cep.get() == "":
  269.             tkMessageBox.showinfo("Alerta", "Preencha o Campo CEP")
  270.  
  271.         elif self.salario.get() == "":
  272.             tkMessageBox.showinfo("Alerta", "Preencha o Campo Salario")
  273.  
  274.         elif self.TelefoneResidencial.get()== "" and self.TelefoneCelular.get() == "":
  275.             tkMessageBox.showinfo("Alerta", "É necessário fornecer pelo menos 1 número de telefone")
  276.  
  277.         elif novofuncionario:
  278.             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+"')"
  279.            
  280.            
  281.            
  282.  
  283.         else:
  284.            
  285.             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
  286.  
  287.         try:
  288.             cursor.execute(sql)
  289.             conexao.commit()
  290.             tkMessageBox.showinfo("Alerta", nome + "Cadastrodo Com sucesso")
  291.         except MySQLdb as erro:
  292.             print("Não COnsegui gravar os arquivos no banco de dados", erro)
  293.  
  294.  
  295.         self.FuncionarioExcluir.configure(state="normal")
  296.         self.FuncionarioAlterar.configure(state="normal")
  297.         self.FuncionarioNovo.configure(state="normal")
  298.         self.GravarFuncionario.configure(state="disabled")
  299.         self.FuncionarioCancelar.configure(state="disabled")
  300.        
  301.         self.desabilitarcampos()
  302.         conexao.close()
  303.  
  304.     def alterar(self):
  305.         self.habilitarcampos()
  306.         self.FuncionarioExcluir.configure(state="disabled")
  307.         self.FuncionarioAlterar.configure(state="disabled")
  308.         self.FuncionarioNovo.configure(state="disabled")
  309.         self.GravarFuncionario.configure(state="normal")
  310.         self.FuncionarioCancelar.configure(state="normal")
  311.         self.comboEstado.configure(state="disabled")
  312.    
  313.         global novofuncionario
  314.         novofuncionario = False
  315.  
  316.  
  317.     def pesquisar(self):
  318.         self.FuncionarioExcluir.configure(state="normal")
  319.         self.FuncionarioAlterar.configure(state="normal")
  320.         conexao = self.ConexaoBanco()
  321.         cursor = conexao.cursor()
  322.  
  323.         if self.PesquisaFuncionario.get()  == "":
  324.             tkMessageBox.showinfo("Alerta", "Digite um Funcionario a ser pesquisado")
  325.            
  326.        
  327.         sql = "select * from funcionarios where nome like '"+self.PesquisaFuncionario.get()+"%'"
  328.  
  329.        
  330.         try:
  331.             cursor.execute(sql)
  332.             dadosFuncionario = cursor.fetchone()
  333.             self.limpacampos()
  334.  
  335.             if dadosFuncionario:
  336.                 #print(dadosPessoa[0])
  337.                 self.CodigoFuncionario.insert(0,dadosFuncionario[0])
  338.                 self.CodigoFuncionario.configure(state="readonly")
  339.                 #self.comboLogin.configure(state="disabled")
  340.                 self.comboEstado.configure(state="disabled")
  341.                 self.NomeFuncionario.insert(0,dadosFuncionario[1])
  342.                 self.EnderecoFuncionario.insert(0,dadosFuncionario[2])
  343.                 self.cep.insert(0,dadosFuncionario[3])
  344.                 self.salario.insert(0,dadosFuncionario[4])
  345.                 self.EmailFuncionario.insert(0,dadosFuncionario[5])
  346.                 #self.comboEstado.insert(0,dadosFuncionario[5])
  347.                 self.CidadeFuncionario.insert(0,dadosFuncionario[7])
  348.                 self.BairroFuncionario.insert(0,dadosFuncionario[8])
  349.                 self.TelefoneResidencial.insert(0,dadosFuncionario[9])
  350.                 self.TelefoneCelular.insert(0,dadosFuncionario[10])
  351.                 self.comboLogin.insert(0,dadosFuncionario[11])
  352.                
  353.  
  354.             else:
  355.                 tkMessageBox.showinfo("Alerta", "Funcionario Não cadastrado")
  356.    
  357.         except MySQLdb as erro:
  358.             print("Não conseguiu listar ",erro)
  359.    
  360.         conexao.close()
  361.  
  362.  
  363.     def excluir(self):
  364.         conexao = self.ConexaoBanco()
  365.         #codigo= int (raw_input("Codigo"))
  366.         ExcluirFuncionario = self.PesquisaFuncionario.get()
  367.         codigofuncionario = self.CodigoFuncionario.get()
  368.         cursor = conexao.cursor()
  369.         sql = "delete from funcionarios where id_funcionario = "+codigofuncionario
  370.         try:
  371.             cursor.execute(sql)
  372.             conexao.commit()
  373.             tkMessageBox.showinfo("Alerta", "Dados Excluidos Com sucesso")
  374.             #print("Dados Excluidos com sucesso")
  375.        
  376.         except MySQLdb as erro:
  377.             tkMessageBox.showerro("Alerta", "Não Foi Possivel Excluir os Dados")
  378.             #print("Não COnsegui Excluir os arquivos no banco de dados", erro)
  379.         self.limpacampos()
  380.  
  381.         conexao.close()
  382.  
  383.  
  384.            
  385.  
  386.  
  387.  
  388.     def ConexaoBanco(self):
  389.         BANCO ="tcc teste"
  390.         USER="root"
  391.         PASSWD=""
  392.         HOST="127.0.0.1"
  393.         try:
  394.             conexao = MySQLdb.connect(db=BANCO,user=USER,passwd=PASSWD,host=HOST)
  395.        
  396.         except:
  397.             print("Conexão com o banco de dados falhou")
  398.        
  399.         return conexao
  400.  
  401.  
  402.     def pesquisarLogin(self):
  403.         conexao = self.ConexaoBanco()
  404.         cursor = conexao.cursor()
  405.         sql = "select login from usuarios "
  406.        
  407.         #if self.EditUsuario.get() == "":
  408.          #   tkMessageBox.showinfo("Alerta", "Digite um Usuario A ser Pesquisado")
  409.         try:
  410.             cursor.execute(sql)
  411.          
  412.             dadosFuncionario = cursor.fetchall()
  413.            
  414.  
  415.             for dados in dadosFuncionario:
  416.                 self.listalogin["values"] = dados
  417.                
  418.                  
  419.                
  420.         except MySQLdb as erro:
  421.             print("Não Consegui Listar:",erro)
  422.  
  423.            
  424.  
  425.         conexao.close()
  426.         print self.listalogin
  427.        
  428.    
  429.  
  430.        
  431.  
  432. def TelaCadastroFuncionario():
  433.     root=Tk()
  434.    
  435.     CadastroFuncionario(root)
  436.     root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement