wxPython的;从远程节点一次发送一个值的值填充网格

时间:2017-10-18 13:41:28

标签: wxpython-phoenix

我有一个远程节点,它在60个不同的寄存器中保存值。我需要在按钮单击时使用值填充网格。我能获得价值的唯一方法是一次一个。这有效,但它很混乱。每次我收到另一个要添加到列表中的值时,我都会收到此错误:" gridArray = np.array(table).reshape(15,4) ValueError:无法将大小数组(列表中的值数)重新整形为形状(15,4)

def onGetValues(self, event):
    self.grid.ClearGrid() 
    for i in range(129,189):  #number of registers will never change
        #send me the data in each of these registers

#Then I build a list as the values are received:

def buildList(self, val):
    global myList
    myList.append(val)      # this gives me a list with one value, then two values, etc
    self.makeArray()  # go to the next step

#Then I turn the final list into an array:

def makeArray(self):
    global myList, gridArray
    gridArray = np.array(myList).reshape(15,4) # grid will never be bigger

#Then use the array to populate the grid:

def popGrid(self):
    global gridArray
    for row in gridArray:
        row_num = row[0]
        cells = row[0:4]
        for i in range(len(cells)):
            if cells [i] != None and cells[i] != "null":
                self.grid.SetCellValue(row_num-1, i, str(cells[i]))

有更好的方法吗?我可以从上一个列表中填充网格并一起跳过数组吗?这可以压缩成一个函数吗?

1 个答案:

答案 0 :(得分:0)

由于列表的长度始终为60:

def buildList(self, val):
    global myList
    myList.append(val)
    if len(myList) == 60:
        self.makeArray()