0
class WelcomeScreen(Screen): #welcomeScreen subclass
    def __init__(self, **kwargs): #constructor method
        super(WelcomeScreen, self).__init__(**kwargs)
        welcomePage = FloatLayout()
        box = BoxLayout(orientation='vertical', size_hint=(0.4, 0.3),
                                       padding=8, pos_hint={'top': 0.5, 'center_x': 0.5})
        self.cameraObject = Camera(play=False)  ## WANT TO USE THIS VARIABLE IN OTHER CLASS
        self.cameraObject.play = True
        self.cameraObject.resolution = (700, 400)
        self.add_widget(self.cameraObject)

class FunctionScreen(Screen):  
    def __init__(self, **kwargs): 
        super(FunctionScreen, self).__init__(**kwargs) #init parent

        functionPage = FloatLayout()
        functionLabel = Label(text='what functions to use',
                          halign='center', valign='center', size_hint=(0.4,0.2), pos_hint={'top': 1, 'center_x': 0.5})
        self.img = self.cameraObject ## HERE I WANT TO USE 'self.cameraObject' VARIABLE

How to get "self.cameraObject" from class WelcomeScreen(Screen) to class FunctionScreen(Screen)? Thanks in advance :)

  • Does this answer your question? [Sharing global variables between classes in Python](https://stackoverflow.com/questions/20181830/sharing-global-variables-between-classes-in-python) – jsub Jul 28 '20 at 09:43
  • Get the cameraObject from your WelcomeScreen obj before creating your FunctionScreen obj? Or a better solution would be to move your cameraObject creation outside of your WelcomeScreen class since it needs to be in both. Then pass it accordingly. – Codesidian Jul 28 '20 at 09:49
  • What you have marked instance attributes, not "class variables". From *which* instance of ``WelcomeScreen`` should *which* instance of ``FunctionScreen`` fetch the attribute ``cameraObject``? – MisterMiyagi Jul 28 '20 at 10:25
  • @MisterMiyagi Im new to python, any help with code is helpfull :/ – Siddanth Shaiva Jul 28 '20 at 10:31
  • @SiddanthShaiva My question is not about writing code, it is about defining what *you* want the code to do. Consider: Which ``cameraObject`` should be used if there is more than one instance of ``WelcomeScreen``? – MisterMiyagi Jul 28 '20 at 10:35
  • @MisterMiyagi I want to use 'self.cameraObject' instance to 'self.img' instance – Siddanth Shaiva Jul 28 '20 at 10:48

2 Answers2

1

I think that your cameraObject should belong to the Screen class or some intermediate abstraction, as it is used by both sub-classes. In this way, the superclass instantiates the cameraObject and any subclass can use it.

Roberto Trani
  • 1,217
  • 11
  • 14
0

You need to take your cameraObject instance out of your classes and pass the single instance of cameraObject to them:

    cameraObject = Camera(play=False)  
    cameraObject.play = True
    cameraObject.resolution = (700, 400)
    WelcomeScreen(foo,bar,cameraObject)
    FunctionScreen(foo,bar,cameraObject)

It's that or create the camera outside of your classes:

cameraObject = Camera(play=False)  
cameraObject.play = True
cameraObject.resolution = (700, 400)

class WelcomeScreen(Screen): #welcomeScreen subclass
    def __init__(self, **kwargs): #constructor method
        super(WelcomeScreen, self).__init__(**kwargs)
            ...
        self.add_widget(cameraObject)

class FunctionScreen(Screen):  
    def __init__(self, **kwargs): 
        super(FunctionScreen, self).__init__(**kwargs) #init parent
            ...
        img = cameraObject 

I also think this might be helpful on the same camera for multiple screens in kivy.

Codesidian
  • 310
  • 2
  • 12