python从一个类到另一个类的列表

问题描述 投票:0回答:2

我想让test_list回到第一个类。如果我想使用self.pushbutton.clicked.connect(self.xy)怎么做,谢谢。

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
python python-3.x list qt class
2个回答
0
投票

你应该让 test_list 一个全局变量。我的意思是你应该在两个类之前声明它,并在类中访问它们。例如

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

然后在返回列表中。

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

一旦你这样做, need_list 可以访问 test_list. 看 此处 了解更多详情。


0
投票

如果您想使用 test_list的方法创建的一个变量。other_window,从一个实例来看 test你有几个选择。

假设你已经定义了一个 test 在全球某个地方,你可以调用 need_list() 并给予 test_list 作为论据。比如说,你也可以让

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())

你也可以做 test_list 全球性的。这将需要更少的变化,但却要付出一些灵活性的代价。

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 有可变的作用域 (见 关于执行的Python文档),也就是说,在你原来的程序中。need_list() 看不见 test_list 因为它是本地定义的。该函数之外的任何东西都无法看到 test_list 除非你把它声明为全局变量(用 global 第二个选项中使用的关键字,它允许在任何地方看到它,除了在重新分配到相同名称的函数体中。请看 这个问题 以了解更多信息)或将其显式传递给一个函数(在 need_list() 如第一个选项所示)。)

希望对大家有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.