无法在 PyQt5 中的网格上创建表
Unable to create tables on a grid in PyQt5
I am trying to create x tables on a GridLayout where x is the size of the list, mylist.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import *
class basicWindow(QWidget):
def __init__(self):
super().__init__()
grid = QGridLayout()
self.setLayout(grid)
mylist = [['Admin',['int a','int b','int c'],
['Student',['int a','int b','int c','int d','int e']]]
for i in range(len(mylist)):
table = QTableWidget()
table.setColumnCount(1)
table.setRowCount(len(mylist[i][1]))
for j in range(len(mylist[i][1])):
table.setItem(j,0,QTableWidgetItem(mylist[i][1][j]))
vBox = QVBoxLayout()
vBox.addWidget(table)
grid.addWidget(vBox,i,0)
self.setWindowTitle('Basic Grid Layout')
if __name__ == '__main__':
app = QApplication(sys.argv)
windowExample = basicWindow()
windowExample.show()
sys.exit(app.exec_())
But everytime I run this code. It gives me the error below. I have tried adding the maximum number of arguments for addWidget method for both Vbox and Grid,respectively. But it also does not seems to be working. Please guide because I'm stuck on this for a while now.
TypeError: arguments did not match any overloaded call:
addWidget(self, QWidget): argument 1 has unexpected type 'QVBoxLayout'
addWidget(self, QWidget, int, int, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] =
Qt.Alignment()): argument 1 has unexpected type 'QVBoxLayout'
addWidget(self, QWidget, int, int, int, int, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] =
Qt.Alignment()): argument 1 has unexpected type 'QVBoxLayout'
从 addWidget()
的文档中应该清楚,该函数要求您使用 QWidget
子类作为第一个参数,而 QVBoxLayout 是不是.
如果要添加布局,则需要使用addLayout()
。
不是很清楚为什么需要使用 QVBoxLayout(因为除了 table 之外没有添加任何其他内容,这使得该布局完全无用),但是,无论如何,只需更改以下内容行:
grid.addWidget(vBox,i,0)
对此:
grid.addLayout(vBox, i, 0)
请始终在逗号后添加一个 space,如官方建议 Python code style guide。
I am trying to create x tables on a GridLayout where x is the size of the list, mylist.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import *
class basicWindow(QWidget):
def __init__(self):
super().__init__()
grid = QGridLayout()
self.setLayout(grid)
mylist = [['Admin',['int a','int b','int c'],
['Student',['int a','int b','int c','int d','int e']]]
for i in range(len(mylist)):
table = QTableWidget()
table.setColumnCount(1)
table.setRowCount(len(mylist[i][1]))
for j in range(len(mylist[i][1])):
table.setItem(j,0,QTableWidgetItem(mylist[i][1][j]))
vBox = QVBoxLayout()
vBox.addWidget(table)
grid.addWidget(vBox,i,0)
self.setWindowTitle('Basic Grid Layout')
if __name__ == '__main__':
app = QApplication(sys.argv)
windowExample = basicWindow()
windowExample.show()
sys.exit(app.exec_())
But everytime I run this code. It gives me the error below. I have tried adding the maximum number of arguments for addWidget method for both Vbox and Grid,respectively. But it also does not seems to be working. Please guide because I'm stuck on this for a while now.
TypeError: arguments did not match any overloaded call:
addWidget(self, QWidget): argument 1 has unexpected type 'QVBoxLayout'
addWidget(self, QWidget, int, int, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] =
Qt.Alignment()): argument 1 has unexpected type 'QVBoxLayout'
addWidget(self, QWidget, int, int, int, int, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] =
Qt.Alignment()): argument 1 has unexpected type 'QVBoxLayout'
从 addWidget()
的文档中应该清楚,该函数要求您使用 QWidget
子类作为第一个参数,而 QVBoxLayout 是不是.
如果要添加布局,则需要使用addLayout()
。
不是很清楚为什么需要使用 QVBoxLayout(因为除了 table 之外没有添加任何其他内容,这使得该布局完全无用),但是,无论如何,只需更改以下内容行:
grid.addWidget(vBox,i,0)
对此:
grid.addLayout(vBox, i, 0)
请始终在逗号后添加一个 space,如官方建议 Python code style guide。