class 个对象的列表不起作用

List of class objects does not work

我写了一个方法,其中我使用一个名为 channelcount 的全局变量和两个名为 listofchannels_1listofchannels_2 的列表。当我按下组合键 (ctrl+shift+c ),一个名为 ViewWidget 的 class 对象(这是一个 matplotlib 图)显示在我的 QMainWindow 中。这样最多进行 3 次。您可以在这张图片中看到:

我想将这个 class 对象添加到 listofchannels_1 中,同时在 listofchannels_2 中添加对象的名称,例如:"Channel 1, Channel 2 and Channel 3"。

这是我写的方法(addChannel):

channel count = 1
listofchannels_1 = []           #These are the global variables
listofchannels_2 = []

Class Window (QMainWindow):
  def __init__(self):
  #A lots of stuff in here


  def addChannel(self):
    global channelCount, listofchannels_1, listofchannels_2
    graphic = ViewWidget() #This is the matplotlib figure (the class object)

    if channelCount <= 3: 
        self.splitter.addWidget(graphic)
        channelCount += 1
        return channelCount

        for i in listofchannels_1:
            listofchannels_1.append(graphic(i))
            for channel in listofchannels_2:
                channel_name[i] = "Channel" + str(channelCount)
                print channel_name[i] 

我还想在每次添加项目时打印两个列表,这样我就可以检查是否在每个列表中创建了一个项目。

当我构建这个时,它不打印任何东西,我不知道这些项目是否按照我的意愿添加到列表中。

我是 python 的新人,有时我不知道我是否正确编写了代码。我究竟做错了什么?还有另一种方法可以不将这些空列表用作全局变量吗?通过这种方式,我可以在以后需要时使用这些列表。

------------ 编辑---------

我修改了 Windowclass 中的方法 addchannel 是这样的:

class Window(QMainWindow):
  def __init__(self):
    self.addChannel()
    self.channelCount = 1
    self.listofchannels_1 = []
    self.listofchannels_2 = []

  def addChannel(self):
    graphic = ViewWidget()
    if self.channelCount <= 3:
      self.splitter.addWidget(graphic)
      channelCount += 1
      self.listofchannels_1.append(graphic(i))
      for i in self.listofchannels_1:
            channel_name[i] = "Channel " + str(self.channelCount)
            self.listofchannels_2.append(channel_name[i])
            print channel_name[i]
    return self.channelCount

现在,我收到一条错误消息:"Window" 对象没有属性 channelCount。我写错了吗?

我怎么知道我是否将 graphic(i) 添加到 listofchannels_1 中?

-------- 编辑 2 ----------

终于成功了。这是最终代码:

class Window(QMainWindow):
  channelCount = 0
  listofchannels_1 = []
  listofchannels_2 = []
  def __init__(self):
    #A lot of stuff in here

  def addChannel(self):
    graphic = ViewWidget() 
    if Window.channelCount <= 2: #Sólo agregaremos hasta 3 canales
        self.splitter.addWidget(graphic)
        Window.channelCount += 1
        Window.listofchannels_1.append(graphic)
        channel_name = "Channel " + str(Window.channelCount)
        Window.listofchannels_2.append(channel_name)
        print channel_name
        print Window.listofchannels_1
        print Window.listofchannels_2
    return Window.channelCount

我将变量 channelCount 和列表放在 init 方法之前,以便不将它们用作全局变量,而是用作实例变量。这是我在尝试解决 mi 问题时学到的东西。

print行只是为了查看列表中是否有元素以及它们是否是正确的元素。所以有了 channelCount 变量;我想看看它是否在改变它的价值。

我可以看到一些错误的地方。

首先,"return"语句将结束函数和return值。在你的函数中途有一个 return 语句意味着该函数的其余部分将永远不会执行。如果你想 return 完成后的东西,把它放在函数的末尾。

在下面的块中,您实际上并没有得到一个我可以看到的名为 channel_name 的变量,您必须先创建它才能开始。

    for i in listofchannels_1:
        listofchannels_1.append(graphic(i))
        for channel in listofchannels_2:
            channel_name[i] = "Channel" + str(channelCount)
            print channel_name[i] 

如果您的频道列表仅在 window class 中使用,则可能值得让它们成为 class 的成员而不是全局成员。您可以使用 self.variableName 设置和访问它们,如:

Class Window (QMainWindow):
    def __init__(self):
        self.channelCount = 1
        self.listofchannels_1 = []          
        self.listofchannels_2 = []

    def printChannelNames(self):
        for channel in self.listofchannels_2:
            print channel