There is no gain or reason to define classes within Python classes. (Ok, there’s actually a very minor reason: you might want to use your classes as namespaces to maintain values)
But in this case, you get nothing. What your "Canvas" will need to have are instances of "Boot" the instances you create when creating your instance of "Canvas" - but I repeat: there is no gain, no benefit, nothing, nothing to define one class within the other. On the other hand, doing so will work against language, and various things under the hood that are there to help you start working against.
Not being able, from within the nested class, to access the above class is just one of those things.
So, what should you do? Keep attributes inside your classes that connect them to the related objects - for example: a button will need to know which screen it is on - so it should receive this information when created.
Look how simple it gets:
class Botao:
def __init__(self, tela):
self.tela = tela
...
def acao(self):
self.tela.acao_global()
class Tela:
def __init__(self):
self.botao_ok = Botao(self)
self.botao_cancela = Botao(self)
def acao_global(self):
# chamada quando o método "acao" de qualquer botao é executado
Is the class button set within the screen class? If so, why?
– Woss
Hi Anderson! So as I’m a beginner, I actually did it more for the sake of getting organized. :)
– Gus