简体   繁体   English

Python函数可在PyQt中填充多个表

[英]Python function to populate multiple tables in PyQt

I have a class with multiple functions and in, almost, everyone of them I have to populate different QTableWidgets. 我有一类具有多个功能的类,几乎每个人都必须填充不同的QTableWidgets。 I would like to make a function that given a tables name, headers and data populates the said table. 我想做一个给定表名,标头和数据填充所述表的函数。

This is working (but static and works just for one table "tableParada"): 这是可行的(但是是静态的,仅适用于一个表“ tableParada”):

header = ['Horario teórico', 'Entrada a terminal', 'Salida de terminal', 'Tiempo parcial en terminal']
self.ui.tableParada.setRowCount(cantidadFilas)
self.ui.tableParada.setColumnCount(len(header))
self.ui.tableParada.setHorizontalHeaderLabels(header)

for columnas in range(cantidadFilas):
    self.ui.tableParada.setItem(columnas, 0, QTableWidgetItem(str(tiempoTeoricoTerminal[columnas])))
    self.ui.tableParada.setItem(columnas, 1, QTableWidgetItem(str(tiempoInicioTerminal[columnas].time())))
    self.ui.tableParada.setItem(columnas, 2, QTableWidgetItem(str(tiempoFinTerminal[columnas].time())))
    self.ui.tableParada.setItem(columnas, 3, QTableWidgetItem(str(tiempoEnTerminal[columnas])))

This is what I came up with (and does not work): 这是我想出的(并且不起作用):

def completarTabla(tabla, headerFila, headerColumna, datos):
    """Completado automático de tabla tipo QTableWidget."""
    tabla.setRowCount(len(headerFila))
    tabla.setColumnCount(len(headerColumna))
    tabla.setHorizontalHeaderLabels(headerColumna)
    tabla.setVerticalHeaderLabels(headerFila)

    for fila in range(len(headerFila)):
        for columna in range(len(headerColumna)):
            tabla.setItem(fila, columna, QTableWidgetItem(str(datos[fila][columna])))

The problem is when I call the function: 问题是当我调用该函数时:

headerColumna = ['Horario teórico', 'Entrada a terminal', 'Salida de terminal', 'Tiempo parcial en terminal']
headerFila = []
completarTabla('tableParada', headerFila, headerColumna, tiempo)

I get: 我得到:

Traceback (most recent call last):
File "C:/Users/TIC/Dropbox/TransBus - La Estrella/Programación/control-de-tiempo/query.py", line 362, in queryParada
completarTabla('tableParada', headerFila, headerColumna, tiempo)
File "C:/Users/TIC/Dropbox/TransBus - La Estrella/Programación/control-de-tiempo/query.py", line 659, in completarTabla
tabla.setRowCount(len(headerFila))
AttributeError: 'str' object has no attribute 'setRowCount'

I know that I am passing a str to the function and then the replacement does not work, if I pass the table name (tableParada) it also does not work. 我知道我将一个str传递给该函数,然后替换不起作用,如果我传递了表名(tableParada),它也将不起作用。 Then: 然后:

  • What should I pass to the function for it to work? 我应该将什么传递给该函数以使其起作用?
  • Is there another method? 还有其他方法吗?
  • Is it even possible? 可能吗

Sorry but I'm new on Python + PyQt, object oriented programming, also this is my first post on stackoverflow and english is not my mother tongue. 抱歉,但是我是Python + PyQt(面向对象编程)的新手,这也是我关于stackoverflow的第一篇文章,英语不是我的母语。 I just really would like to make it in a right way. 我只是真的想以正确的方式做到这一点。

Thanks in advance! 提前致谢!

Try passing self.ui.tableParada instead of the string 'tableParada' . 尝试传递self.ui.tableParada而不是字符串'tableParada' You need to pass the actual widget object. 您需要传递实际的小部件对象。

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

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