【问题标题】:python get list from one class to anotherpython从一个类到另一个类获取列表
【发布时间】:2020-08-05 23:14:38
【问题描述】:

我正试图让 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 pyqt pyqt5


    【解决方案1】:

    您应该将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。更多详情请关注here

    【讨论】:

      【解决方案2】:

      如果您想使用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 具有可变范围(请参阅the Python documentation on execution),这意味着在您的原始程序中,need_list() 无法看到test_list,因为它是在本地定义的。该函数之外的任何内容都无法看到test_list,除非您将其声明为全局变量(在第二个选项中使用global关键字,这使得它可以在除重新分配给相同名称的函数体之外的任何地方看到. 请参阅this question 了解更多信息)或将其显式传递给函数(使用need_list() 中的函数参数,如第一个选项所示)。

      希望这有帮助!

      【讨论】:

      • 需要注意的是,完全转载原代码并不是一个好主意,这是因为第二个插槽连接根本不会做任何事情。我建议您编辑帖子,以便它报告。我还要添加建议,以避免在从同一信号创建连接时连接到信号(或者,至少确保信号仅连接一次):如果多次单击按钮,则会导致在第二个信号中,每次都会再次调用一次。
      • 我想过删除未更改的代码,但我不确定要保留哪些部分。我认为你有为我编辑我的帖子的声誉,因为你似乎比我更清楚该做什么。
      • 我真的不喜欢编辑人们的答案(最重要的是,他们的代码),除非我认为确实有必要,因为我担心它会改变答案的初衷和意义,也因为我认为自我编辑提供了很多见解。我会保留你的答案,因为我认为我的评论可能足以让未来的读者提供更多信息(这就是 cmets 的用途),并且,如果有一天你会记住它并找时间编辑它,这将意味着其他人(特别是您,但不仅仅是您)将从中获得更多好处:-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      相关资源
      最近更新 更多