-2

My problem in the simplified version is the following:

class FirstWindow(Screen):
    koszyk = ""
    #class Koszyk has 2 attributes: Lista_produktów, Wagi_produktów

    def generate_basket(self):
        [...] 
        koszyk = Koszyk(list_of_products,weights)
        self.koszyk = koszyk

    def show_result(self):
        print(self.koszyk.Lista_produktów)
        print(self.koszyk.Wagi_produktów)
 
class SecondWindow(Screen):
    def generate(self):
        print(FirstWindow.koszyk.Wagi_produktów)

Whenever I am printing self.koszyk in FirstWindow, it works and it shows the values, whenever I try to do the same in SecondWindow using generate() function, it doesn't work. Error that I am having is:

AttributeError: 'str' object has no attribute 'Wagi_produktów'

How to fix my problem?

  • 1
    Show the error. Also, show a [mcve]. Your example is not particularly useful because it's missing most of the important bits. – Mad Physicist Jan 16 '23 at 17:28
  • 3
    Note that the class attribute `FirstWindow.koszyk` and the instance attribute `koszyk` assigned to instances of `FirstWindow` are separate attributes on different objects. – Brian61354270 Jan 16 '23 at 17:28
  • My question might be a bit missleading, but code is really big and I cannot propely replicate the example - in generate_basket() function I am creating a class based on list of arguments prepared in this function and I am passing it to the class attribute FirstWindow.koszyk. I cannot access that outside of the class, because it seems like it didn't assign the created instance there. – Michał Mazur Jan 16 '23 at 17:33
  • Do you want all instances of `FirstWindow` and all instances of `SecondWindow` to share the same koszyk / shopping cart? – CrazyChucky Jan 16 '23 at 17:37
  • I want to have access to FirstWindow.koszyk in SecondWindow. FirstWindow.koszyk is prepared using generate_basket() function and the FirstWindow.koszyk is assigned Koszyk class instance instead of "", but then I want access it from outside of the FirstWindow class – Michał Mazur Jan 16 '23 at 17:40
  • 1
    @MichałMazur: `self.koszyk = koszyk` you are assigning `koszyk` to the instance **not** class. – Maurice Meyer Jan 16 '23 at 17:42
  • but line before I use: koszyk = Koszyk(list_of_products,weights) – Michał Mazur Jan 16 '23 at 17:43
  • As currently structured, you wouldn't use `FirstWindow.koszyk`, you'd use whatever you name your instance. Like `window = FirstWindow()` then `window.koszyk`. But what you're doing doesn't seem to make a lot of sense. I think you need to read a tutorial or two on object-oriented programming, and what classes are for. – CrazyChucky Jan 16 '23 at 17:44
  • 1
    @MichałMazur: Have a look at: [python class instance variables and class variables](https://stackoverflow.com/questions/8701500/python-class-instance-variables-and-class-variables) – Maurice Meyer Jan 16 '23 at 17:48
  • It still doesn't help me, because both of those aren't particularly regular classes I dealt with, but Screens created using Kivy. I want to create a "koszyk" in one screen and then move on to the second screen (class in this case) and get access to it here. – Michał Mazur Jan 16 '23 at 17:51
  • 1
    So your question is how do you share data between multiple Kivy screens? – CrazyChucky Jan 16 '23 at 17:53
  • Probably yes, I don't know how to formulate this question well. That's what I want, to have access on a second scree, while the function prepares the value in first screen. – Michał Mazur Jan 16 '23 at 17:54
  • It would probably be a good idea to [edit] your question and provide that context. – CrazyChucky Jan 17 '23 at 12:34

1 Answers1

0

I partially solved the problem, but I would like to see why the other way around doesn't work.

class FirstWindow(Screen):
    #class Koszyk has 2 attributes: Lista_produktów, Wagi_produktów

    def generate_basket(self):
        [...] 
        koszyk = Koszyk(list_of_products,weights)
        SecondWindow.koszyk = koszyk
 
class SecondWindow(Screen):
    koszyk = ""
    def generate(self):
        print(self.koszyk.Wagi_produktów)

  • 1
    Because you're setting an attribute of the *class* this time, not an *instance*. A class and an instance *of* that class are not the same thing. All dogs are dogs, but only one dog is *my* particular dog. Giving my dog a collar doesn't give *all* dogs a collar. But giving *all* dogs a collar *would* give my dog a collar, because my dog is a dog. – CrazyChucky Jan 16 '23 at 22:42