Ir para conteúdo
Fórum Script Brasil
  • 0

Problema com Classes e Pacotes no python


GiovanniScripter

Pergunta

Bom estou com um problema na hora de chamar um atributo em um outro arquivo, ou seja eu estou trabalhando com pacotes o primeiro pacote(arquivos diferentes) é o principal com nome de Gerenciador.py e nele tem esse código:

from Tkinter import *
from constantes import *
from BD import *

class main():
  def __init__(self, principal):

    self.frame1 = Frame(principal)
    self.frame1.pack()
    self.BCriar = Button(self.frame1, width = 10, command = self.criar, text = 'Criar')
    self.BCriar.pack(side = LEFT, pady = '10px')
    self.BGerenciar = Button(self.frame1, width = 10, command = self.gerenciar, text = 'Gerenciar')
    self.BGerenciar.pack(side = LEFT, pady = '10px')
    self.BDeletar = Button(self.frame1, width = 10, command = self.deletar, text = 'Deletar')
    self.BDeletar.pack(side = LEFT, pady = '10px')
    self.BAjuda = Button(self.frame1, width = 10, command = self.ajuda, text = 'Ajuda')
    self.BAjuda.pack(side = LEFT, pady = '10px')
    self.BSobre = Button(self.frame1, width = 10, command = self.sobre, text = 'Sobre')
    self.BSobre.pack(side = LEFT, pady = '10px')

  def criar(self):

   self.BCriar.pack_forget()
   self.BGerenciar.pack_forget()
   self.BDeletar.pack_forget()
   self.BAjuda.pack_forget()
   self.BSobre.pack_forget()
   criarBanco()

  def gerenciar(self):
    pass
  def deletar(self):
    pass
  def ajuda(self):
    pass
  def sobre(self):
    pass

 principal = Tk()
 main(principal
 principal.geometry('800x600')
 principal.resizable(False, False)
 principal.title('Gerenciador De Cadastro')
 principal.mainloop()

Quando clicar no botao criar ele entra na proxima página, que no caso é esse codigo com nome de BD.py(outro arquivo):
from Tkinter import *
from sqlite3 import *
from constantes import *
from Criacao import *
class criarBanco(object):
  def __init__(self, principal):
#frames e empacotamento de frames
    self.font = ('Arial', '14', 'bold')
    self.font1 = ('Arial', '10', 'bold')
    self.frame0 = Frame(principal)
    self.frame0.pack(pady = padyFrame0)
    self.frame1 = Frame(principal)
    self.frame1.place()
    self.frame1.pack()
    self.frame1['bg'] = bgFrame1
    self.frame5 = Frame(principal)
    self.frame5.pack(pady = padyFrame0)
    self.frame2 = Frame(principal)
    self.frame2.place()
    self.frame2.pack()
    self.frame2['bg'] = bgFrame2
    self.frameBotEnviar = Frame(principal)
    self.frameBotEnviar.place()
    self.frameBotEnviar.pack(pady = padyBotEnviar)
##Texto de Aviso de Marcado Ou no
    self.nomeMarcado = Label(self.frame5, text = 'Nome = No ', font = self.font1)
    self.nomeMarcado.pack(side = LEFT)
    self.corMarcado = Label(self.frame5, text = ' Cor = No ', font = self.font1)
    self.corMarcado.pack(side = LEFT)
    self.cpfMarcado = Label(self.frame5, text = ' CPF = No  ', font = self.font1)
    self.cpfMarcado.pack(side = LEFT)
    self.emailMarcado = Label(self.frame5, text = ' Email = No', font = self.font1)
    self.emailMarcado.pack(side = LEFT)


#Variaveis CheckButton
    self.Vnome = IntVar()
    self.Vcor = IntVar()
    self.Vcpf = IntVar()
    self.Vemail = IntVar()

#################

    self.L1 = Label(self.frame1, font = self.font, text = "  Nome do Seu Banco de Dados  ", bg = bgNomeDoBanco)       
    self.L1.pack()
    self.LL1 = Label(self.frame1, bg = '#B5B5B5')
    self.LL1.pack()
    self.E1 = Entry(self.frame1, bd = 5, highlightcolor = '#1E90FF')
    self.E1.pack()          
    self.L2 = Label(self.frame1, font = self.font, text = ' Digite a Senha do seu Banco de Dados ', bg = bgNomeDoBanco)
    self.L2.pack()
    self.E2 = Entry(self.frame1, show = '*',  bd = 5, highlightcolor = '#1E90FF')
    self.E2.pack() 
    self.L3 = Label(self.frame1, font = self.font, text = ' Confirme a Senha do seu Banco de Dados ', bg = bgNomeDoBanco)
    self.L3.pack()
    self.E3 = Entry(self.frame1, show = '*', bd = 5, highlightcolor = '#1E90FF')
    self.E3.pack()


## CheckButtons

    self.nome = Checkbutton(self.frame2, bg = bgCheckButton, font = self.font1, command = self.PegarValor, bd = 3, text = 'Nome', onvalue = 1, offvalue = 0, variable = self.Vnome)
    self.nome.pack(side = LEFT)
    self.cor = Checkbutton(self.frame2, bg = bgCheckButton,  font = self.font1, command = self.PegarValor, bd = 3, text = 'Cor', onvalue = 1, offvalue = 0, variable = self.Vcor)
    self.cor.pack(side = LEFT)
    self.cpf = Checkbutton(self.frame2, bg = bgCheckButton,  font = self.font1, command = self.PegarValor, bd = 3, text = 'CPF', onvalue = 1, offvalue = 0, variable = self.Vcpf)
    self.cpf.pack(side = LEFT)   
    self.email = Checkbutton(self.frame2, bg = bgCheckButton,  font = self.font1, command = self.PegarValor, bd = 3, text = 'Email', onvalue = 1, offvalue = 0, variable = self.Vemail)
    self.email.pack(side = LEFT)
    self.BotEnviar = Button(self.frameBotEnviar, command = self.enviaBanco, bg = '#CFCFCF',  text = 'Enviar', font = self.font1, bd = 5)
    self.BotEnviar.pack()

#################
  def PegarValor(self): 
   v = [0,0,0,0]

   if self.Vnome.get() == 1:
    v[0] = 1
    self.nomeMarcado['text'] = 'Nome = Sim  '
   else: 
    self.nomeMarcado['text'] = ' Nome = No  '
   if self.Vcor.get() == 1:
      v[1] = 1
      self.corMarcado['text'] = ' Cor = Sim  '
   else:
    self.corMarcado['text'] = ' Cor = No  '
   if self.Vcpf.get() == 1:
      v[2] = 1
      self.cpfMarcado['text'] = ' CPF = Sim  '
   else:
      self.cpfMarcado['text'] = ' CPF = No  '
   if self.Vemail.get() == 1:
      v[3] = 1
      self.emailMarcado['text'] = ' Email = Sim  '
   else:
      self.emailMarcado['text'] = ' Email = No'

  def enviaBanco(self):
   if self.E2.get() == self.E3.get():
    self.frame0.pack_forget()
    self.frame1.pack_forget()
    self.frame2.pack_forget()
    self.frame5.pack_forget()
    self.nomeMarcado.pack_forget()
    self.corMarcado.pack_forget()
    self.cpfMarcado.pack_forget()
    self.emailMarcado.pack_forget()
    self.frameBotEnviar.pack_forget() 
    self.BotEnviar.pack_forget()
    self.L1.pack_forget()
    self.LL1.pack_forget()
    self.L2.pack_forget()
    self.L3.pack_forget()
    self.E1.pack_forget()
    self.E2.pack_forget()
    self.E3.pack_forget()
    self.nome.pack_forget()
    self.cor.pack_forget()
    self.cpf.pack_forget()
    self.email.pack_forget()
    inicioBanco(principal) 
 else:
    self.E2.delete(0, END) 
    self.E3.delete(0, END)

###############

Assim que voce colocar o nome e a senha e confirmar a senha e apertar enviar ele vai para esse arquivo com nome de Criacao.py

from Tkinter import *

class inicioBanco(object):
 def __init__(self, principal):
  self.frame11 = Frame(principal)
  self.frame11.pack()
  self.frame22 = Frame(principal)
  self.frame22.pack()
  usuarios = Label(self.frame11, text = 'Quantas Pessoas Voce Quer Cadastrar ?')
  usuarios.pack()
  self.entUsuarios = Entry(self.frame11, bd = 5, highlightcolor = '#1E90FF')
  entUsuarios.pack()
  Benviar = Button(self.frame22, text = 'Enviar', bd = 5)
  Benviar.pack()

ai quando vai para o arquivo acima, da o seguinte erro:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1540, in __call__
    return self.func(*args)
  File "/home/giovanni/Área de Trabalho/Python/BD.py", line 122, in 
enviaBanco
    inicioBanco(principal)
NameError: global name 'principal' is not defined

Eu sei que é porque o nome principla não esta definido mas eu não consigo defini-lo pois principal é a instancia de Tk() então se eu colocar:

principal = Tk()
principal.geometry('800x600')
principal.resizable(False, False)
principal.title('Gerenciador De Cadastro')
principal.mainloop()

Ira ficar abrindo varias janela. Então Se entenderem me ajudem preciso muito. Lembrando CADA BLOCO É UM ARQUIVO COM NOME DIFERENTER O PRIMEIRO BOLBO É Gerenciador.py O SEGUNDO É BD.py E O TERCEIRO Criacao.py

Editado por GiovanniScripter
Titulo digitado errado
Link para o comentário
Compartilhar em outros sites

0 respostass a esta questão

Posts Recomendados

Até agora não há respostas para essa pergunta

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,9k
×
×
  • Criar Novo...