Home > Net >  Call variables from another class- python
Call variables from another class- python

Time:06-29

I want the hello function to call and access variables from secondscreen,but it is showing that 'function' object has no attribute 'count',please help me

  class SecondScreen(Screen):
      def __init__(self, **kwargs):
         super(SecondScreen, self).__init__(**kwargs)
         self.count = 1
      def buttonClicked(self):
         self.count  = 1
         count=self.count
         newButt = MDTextField( pos_hint={'center_x': 0.8}, width=100) 
         self.ids.box.add_widget(newButt, index=1)
   class CameraScreen(Screen):
        def hello(self):
           for i in range(SecondScreen.buttonClicked.count):
             var1=SecondScreen.buttonClicked.newButt.text
             var1=var1.lower()
             var2=CameraScreen.take_picture.boxes.split()
             var2=var2.lower()
             common=var1 & var2
             print(common)

CodePudding user response:

You seem to be quite confused about the roles of classes, methods, and attributes. Perhaps as a first measure, let's refactor your code so it does something useful (and hopefully more or less similar to what you want, though that's harder to guess).

class SecondScreen(Screen):
 def __init__(self, **kwargs):
    super(SecondScreen, self).__init__(**kwargs)
    self.count = 1
    self.new_button = None
 def buttonClicked(self):
    self.count  = 1
    self.new_button = MDTextField( pos_hint={'center_x': 0.8}, width=100) 
    self.ids.box.add_widget(newButt, index=1)

class CameraScreen(SecondScreen):
 def hello(self):
    for i in range(self.count):
        var1 = self.new_button.text.lower() # will fail if self.new_button is None
        var2 = self.take_picture.boxes.lower().split() # can't lower() the result of .split()
        common = var1 & var2
        print(common)

Notice how CameraScreen is a subclass of SecondScreen, so that it inherits its behavior, and how the attributes belong to self, not to the class.

(Without seeing how these are used in more detail, it's hard to say whether this is correct. There is a way for an attribute to belong to a class, and then be shared by all instances of that class. To very briefly recap, CameraScreen() creates a new instance of the class, with a self which is distinct from all other instances.)

Incidentally, split returns a list, so there is quite probably more work to do around var2; but again, without any explanation or guidance for what the code should actually do, it's hard to say more than that. Should var2 perhaps only contain the first (or the last, or the 42nd?) result from split?

If SecondScreen has many additional methods and attributes which you don't want to inherit, maybe split it up into two classes:

class OneAndAHalfScreen (Screen):
    """
    All the methods and attributes which should be
    inherited by both SecondScreen and CameraScreen
    """
    ... 

class SecondScreen(OneAndAHalfScreen):
    ...

class CameraScreen(OneAndAHalfScreen):
    ...

The code from SecondScreen in your question would all go in OneAndAHalfScreen so that CameraScreen will also inherit it, but any code which it should not inherit would still belong in the SecondScreen class.

CodePudding user response:

Variables declared in classes and functions are local variables, For eg.

x = 5
def hello():
    x=6
    print(x) //outputs 6
print(x) //outputs 5

So what you might want to do is to create the count variable global using the global keyword

global self.count 
self.count=1
  • Related