Problem with Classes and Packages in python

1

I asked a question but I decided to improve it to have a better understanding, and also because nobody could answer for the fact that there is little explanation in this one I will be talking about the packages, ie for each block and a file with a different name, so if you put everything inside one will not work. Hi, I have a problem in calling an attribute in another file, ie I'm working with packages. The first package (different files) is the main one with the name of Manager.py and it has this code:

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()

When you click on the create button it will enter the next page, which in the case is this code named BD.py (another file):

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)

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

As soon as you enter the name and password and confirm the password and press send it goes to this file with name of Creation.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()

when it goes to the above file, it gives the following error:

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

I know it's because the main name is not defined but I can not define it because main is the Tk () instance so if I put:

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

Wanna be opening several windows. So if you understand me help me I need a lot. Remembering EVERY BLOCK IS A FILE WITH NAME DIFERENTER THE FIRST BOLBO IS A MANAGER.py THE SECOND IS BD.py AND THE THIRD CREATION.py

    
asked by anonymous 20.05.2017 / 16:58

1 answer

0

In the init definition of the main function (In python, set class names in camel-case, I suggest that a PEP8 ) def __init__(self, principal): do you send the" main "parameter from somewhere right?

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

    self.frame1 = Frame(principal)
    self.frame1.pack()
    ...

Then, below, in the same class you define the create function:

 def criar(self):

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

The create bank class has the init function with the following signature:

class criarBanco(object):
  def __init__(self, principal):
  .....

Ok, but when you call criarBanco (Again I suggest you look at the encoding formatting guide PEP8 , would be more "pythonic" if the name of this class was CriarBanco ) you do not send the main parameter, so how does this function know this parameter?

You can fix this in two ways: Make global the main variable in the scope of the package where it was created and import it into every package you need or send it "from hand to hand" to every function call that is used.

You can do the following test: Just in the first line of each function that you need the variable "principal", make a print like this:

print (principal==None)

And check the result.

    
20.05.2017 / 18:18