1

I'm creating with PyQt5 a program that creates wordclouds.

The program has: 1. a main window that gets the text and generate a wordcloud 2. a settings window that has 3 color options

I need to pass the user chosen color from the settings window to the main window. For that, i've created 3 classes:

class A:

    def __init__(self, colormap):
           self.colormap = colormap

    def set_colormap(self, x):
           self.colormap = x

    def get_colormap(self):
           return self.colormap

#in the real program this class represents the second window with the color pick options:#
class B(A):

    def __init__(self):
        A.__init__(self, "blue")
           self.c = A("blue")

    def userColorChoice(self):
           userinput = input("Choose ColorMap: \n")
#in the real program there are 3 button for options instead of this input#
           c.set_colormap(userinput)

And:

from test2 import B

#in the real program this represnts the class of the main window which includes the create wordcloud function:#
class C(B):

    def __init__(self):
            Func = B()
            self.ChosenColorMap = Func.c.get_colormap()

    def create_wordcloud(self):
            #here i'm using the self.ChosenColorMap#

My problem is that when i use the self.ChosenColorMap in my class C it gets the default value of self.c (in this case: "blue") and not the user choice.

I think that the problem is in Func = B() in class C because whenever i call it, it initialize self.c to "blue".

How can i fix it?

Thanks

2
  • Where do you call the function that asks the user for their color choice? Would you not just want to call self.userColorChoice() and then look at self.get_colormap()? Commented Mar 4, 2018 at 18:05
  • self.userColorChoice() is connected to a button. Whenever the user clicks on the button, the color choice changes Commented Mar 4, 2018 at 18:13

1 Answer 1

2

You do have a couple of issues in your code. Let's start with your C class. Here you create an object of class B named Func, and ask for its color map. The reference in Func isn't stored anywhere. So as soon as your __init__(self) method in your C class completes, the reference in Func is lost. You should initiate your C class similarly to how you initiate your B class, by actually initiating the parent, class A. Your assignment of self.c is still most likely not what you want.

Let's look at your class B. You initiate the parent, then assign self.c a new instance of A. This means that self.c doesn't reference the same A object that you inherit from.

Based on this I'd say you need to look over how to initiate your object hierarchies properly to fix this. For Python 3 you can take a look at https://www.python-course.eu/python3_inheritance.php to start with. How to do this changed when Python 3 saw the light of day. Python 2 does it differently, see https://www.python-course.eu/inheritance_example.php for Python 2. There are of course other sites describing this to a varying degree.

Sign up to request clarification or add additional context in comments.

1 Comment

If you use class A(object): you can use the Python 3 thing iirc.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.