繁体   English   中英

python 从一个 class 获取列表到另一个

[英]python get list from one class to another

我试图让 test_list 回到第一个 class。 如果我想使用 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

您应该使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 在这里查看更多详细信息。

如果你想使用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 ,因为它是在本地定义的。 function 之外的任何内容都无法看到test_list除非您将其声明为全局变量(在第二个选项中使用global关键字,这允许它在除重新分配给相同名称的函数体之外的任何地方看到。请参阅此问题有关此的更多信息)或将其显式传递给 function(如第一个选项中所示,在need_list()中使用 function 参数)。

希望这有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM