gpt4 book ai didi

python - 从列表中有条件地创建 Groupboxes

转载 作者:太空宇宙 更新时间:2023-11-04 01:58:18 25 4
gpt4 key购买 nike

我有以下自动生成的列表,我想用它们用 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,它看起来像一个带有以下代码的表格:

Unified_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()

但是,我想创建 Groupboxes,当第一个列表中的相同“国家/地区”连续出现时,它会组合对应于该国家/地区的元素。

通过查看下面我打算做的示例来形象化它可能会更好:

Separated_groupboxes

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

最佳答案

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

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()

关于python - 从列表中有条件地创建 Groupboxes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56283847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com