-3

I'm trying to get the test_list back to the first class. How can I do that if i want to use the self.pushbutton.clicked.connect(self.xy) thanks

class test(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
       self.test.setupUi(self) # QT
       #do something
       self.pushbutton.clicked.connect(self.connect1)

    def connect1(self):
        window = other_window(self)
        otherview.show()

    def need_list(self):
        print(test_list)

class other_window(QtWidgets.QMainWindow)
    def __init__(self, parent=None):
        self.other_window.setupUi(self) # QT
        #do something
        self.pushbutton.clicked.connect(self.connect2)

    def connect2(self):
        # do something
        self.pushbutton.clicked.connect(self.return_list)

    def return_list(self):
        test_list = []
        test_list.append("a", "b", "c")
        # return test_list to need_list
eyllanesc
  • 235,170
  • 19
  • 170
  • 241

2 Answers2

0

You should make test_list a global variable. By that I mean you should declare it before both classes and access them in the classes. Example:

test_list = []
class test(QtWidgets.QMainWindow):
    ...

and then in return list:

def return_list(self):
    global test_list
    # do what you want with it

Once you do this, need_list can access test_list. Look here for more details.

xilpex
  • 3,097
  • 2
  • 14
  • 45
0

If you want to use test_list, a variable created by a method of other_window, from an instance of test, you have a couple options.

Assuming you've defined an instance of test globally somewhere, you can call need_list() and give test_list as an argument. For example:

class test(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
       self.test.setupUi(self) # QT
       #do something
       self.pushbutton.clicked.connect(self.connect1)

    def connect1(self):
        window = other_window(self)
        otherview.show()

    def need_list(self, test_list):
        print(test_list)

class other_window(QtWidgets.QMainWindow)
    def __init__(self, parent=None):
        self.other_window.setupUi(self) # QT
        #do something
        self.pushbutton.clicked.connect(self.connect2)

    def connect2(self):
        # do something
        self.pushbutton.clicked.connect(self.return_list)

    def return_list(self):
        test_list = []
        test_list.append("a", "b", "c")
        return test_list

test_instance = test()  # Or however you defined these
other_window_instance = other_window()

test_instance.need_list(other_window_instance.return_list())

You could also make test_list global. This would require less change at the cost of some flexibility:

class test(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
       self.test.setupUi(self) # QT
       #do something
       self.pushbutton.clicked.connect(self.connect1)

    def connect1(self):
        window = other_window(self)
        otherview.show()

    def need_list(self):
        print(test_list)

class other_window(QtWidgets.QMainWindow)
    def __init__(self, parent=None):
        self.other_window.setupUi(self) # QT
        #do something
        self.pushbutton.clicked.connect(self.connect2)

    def connect2(self):
        # do something
        self.pushbutton.clicked.connect(self.return_list)

    def return_list(self):
        global test_list

        test_list = []
        test_list.append("a", "b", "c")

test_instance = test()
other_window_instance = other_window()

Python has variable scopes (see the Python documentation on execution), meaning that in your original program, need_list() is unable to see test_list because it is defined locally. Anything outside that function is unable to see test_list unless you declare it a global variable (with the global keyword used in the second option, which allows it to be seen anywhere except in the bodies of functions that reassign to the same name. See this question for more about this) or pass it to a function explicitly (using a function argument in need_list() as demonstrated in the first option).

Hope this helped!

TechPerson
  • 320
  • 1
  • 8
  • 2
    It should be noted that the full repost of the original code is not a good idea, and that's due to the fact that the second slot connection won't do anything at all. I'd suggest you to edit the post so that it reports that. I'd also add the suggestion to avoid connecting to a signal whenever the connection is created from the same signal too (or, at least, ensure that the signal is connected only once): if the button is clicked multiple times, that would result in the second signal being called once more each time. – musicamante Apr 22 '20 at 21:49
  • I thought about removing the unchanged code, but I wasn't sure exactly what parts to keep. I think you have the reputation to edit my post for me, since you seem to know what to do better than I do. – TechPerson Apr 22 '20 at 22:21
  • 1
    I really don't like to edit people's answers (and, most importantly, their code) unless I believe it's really necessary, since I'm afraid it could change the original intent and sense of the answer, and also because I think that self-editing offers lots of insights. I'll leave your answer as it is, as I think that my comment might be enough for future readers to offer more information (that's what comments are for), and, if someday you'll remember it and find the time to edit it, it will mean that somebody else (you, specifically, but not only) will get some more benefit from it :-) – musicamante Apr 22 '20 at 22:31