类对象列表不起作用

时间:2016-01-26 10:54:42

标签: python list pyqt4

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

enter image description here

我想将此类对象添加到listofchannels_1,同时为listofchannels_2添加对象的名称,例如:“频道1,频道2和频道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中的新手,有时我不知道我是否正确编写了代码。我究竟做错了什么?还有另一种不使用这些空列表作为全局变量的方法吗?通过这种方式,我可以在以后使用这些列表,如果需要的话。

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

我修改了addchannel类中的方法Window,如下所示:

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变量;我想知道它是否正在改变它的价值。

1 个答案:

答案 0 :(得分:2)

我可以看到一些错误的事情。

首先,“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] 

如果频道列表仅用于窗口类中,则可能值得使它们成为类的成员而不是全局成员。您可以使用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