2

我有以下自动生成的列表,我想使用 PyQt5 创建分组框:

Country = ['France', 'France', 'Germany', 'Austria', 'Austria','Spain', 'Portugal','Portugal','Portugal','Spain']
Date = ['26.04', '14.05', '25.02', '18.04','15.06','18.09', '15.05', '28.03', '09.09', '10.10']
Lines = ['Line1', 'Line2', 'Line3', 'Line4', 'Line5', 'Line6', 'Line7', 'Line8', 'Line9', 'Line10']

我知道我可以得到这种类型的 Groupbox,它看起来像一个带有以下代码的表格:

统一组框

import sys
from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()

        Country = ['France', 'France', 'Germany', 'Austria', 'Austria','Spain', 'Portugal','Portugal','Portugal','Spain']
        Date = ['26.04', '14.05', '25.02', '18.04','15.06','18.09', '15.05', '28.03', '09.09', '10.10']
        Lines = ['Line1', 'Line2', 'Line3', 'Line4', 'Line5', 'Line6', 'Line7', 'Line8', 'Line9', 'Line10']


        self.setGeometry(100, 100, 800, 600)
        self.setWindowTitle('Window')
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainB  = QHBoxLayout()


        self.GB1 = QGroupBox("")
        self.GB1.setStyleSheet('QGroupBox {'
                 'border: 0px solid gray; }')
        self.GB1.setFixedHeight(250)
        self.GB1.setFixedWidth(250)

        self.mainB.addWidget(self.GB1)

        grid1 = QGridLayout()
        for i in range(0,len(Country)):
           grid1.addWidget(QLabel(Country[i]), i, 0)
           grid1.addWidget(QLabel(Date[i]), i, 1)
           grid1.addWidget(QLabel(Lines[i]), i, 2)


        self.GB1.setLayout(grid1)

        self.centralWidget.setLayout(self.mainB)


def main():
    app = QApplication([])
    mainWin = MainWindow()
    mainWin.show()
    app.exec_()

if __name__ == '__main__':
   main()

但是,我想创建组框,当该国家连续出现时,它将组合与第一个列表中的相同“国家”相对应的元素。

通过查看我打算在下面执行的示例来将其可视化可能会更好:

分隔组框

有没有办法自动生成这些组框以达到预期的结果?

4

1 回答 1

2

解决方案的关键是对数据进行分组,在这种情况下,我使用一个字典,其中每个键是国家,它的值是日期和行元组的列表。

import sys
from PyQt5 import QtGui, QtCore, QtWidgets


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        Country = ['France', 'France', 'Germany', 'Austria', 'Austria','Spain', 'Portugal','Portugal','Portugal','Spain']
        Date = ['26.04', '14.05', '25.02', '18.04','15.06','18.09', '15.05', '28.03', '09.09', '10.10']
        Lines = ['Line1', 'Line2', 'Line3', 'Line4', 'Line5', 'Line6', 'Line7', 'Line8', 'Line9', 'Line10']

        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)

        lay = QtWidgets.QVBoxLayout(central_widget)

        d = {}
        for country, date, line in zip(Country, Date, Lines):
            if country in d:
                d[country].append((date, line))
            else:
                d[country] = [(date, line)]

        for country, content in d.items():
            groub_box = QtWidgets.QGroupBox(country)
            grid_layout = QtWidgets.QGridLayout()
            groub_box.setLayout(grid_layout)
            lay.addWidget(groub_box)

            for row, (date, line) in enumerate(content):
                grid_layout.addWidget(QtWidgets.QLabel(date), row, 0)
                grid_layout.addWidget(QtWidgets.QLabel(line), row, 1)


def main():
    import sys

    app = QtWidgets.QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()
于 2019-05-23T23:10:11.793 回答