Problem with Classes and Packages in python

Asked

Viewed 275 times

1

I asked a question but resolved to improve it to have a better understanding,and also because no one could answer the fact that little explanation in this one I will be talking about the packages or is for each block and a file with a different name so if you put everything within one will not work. Well I have a problem when calling an attribute in another file, I mean I am working with packages the first package(different files) is the main one named Manager.py and in 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 the create button it enters the next page, which in this case is this code with BD.py name (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 that file named 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()

ai when it goes to the file above, 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 is because the princely name is not defined but I cannot define it because the main instance is Tk() so if I put:

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

It will keep opening several windows. So if you understand help me I need a lot. Remembering EACH BLOCK IS A FILE WITH A DIFFERENT NAME THE FIRST BULB IS MANAGER.py THE SECOND IS BD.py AND THE THIRD CREATION.py

1 answer

1

In defining init of function main (In python, define class names in Camel-case, I suggest a read on PEP8) def __init__(self, principal): you send the "main" parameter from somewhere right?

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

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

Ai, further down, 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):
  .....

Okay, but when you call criarBanco (Again I suggest you take a look at the coding formatting guide PEP8, would be more "pythonico" if the name of that class was CriarBanco) you do not send the main parameter, so how this function will know this parameter?

You can fix this in two ways: Making the main variable global in the scope of the package where it was created and importing it into each package you need or sending it "hand-in-hand" to each function call that uses it.

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

print (principal==None)

And check the result.

  • How so make the main variable in scope ? and send it from hand to hand ?

  • Kept appearing false, but still can not solve

  • It is as if the program could not pass to the other Creation window

  • When I import it the program opens two windows the first of an error and the second continues as if nothing has changed

  • Actually I was wrong in typing, already corrected, what I meant is to "make the main variable global" in the scope where it is created or send it as parameter every time you call some function q need it.

  • You could help me with this because I can’t at all

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.