从列表中有条件地创建 Groupboxes
Create Groupboxes conditionally from lists
我有以下自动生成的列表,我想用它们用 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']
我知道我可以使用以下代码得到这种看起来像 table 的 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,当第一个列表中的 'Country' 国家连续出现时,它会合并对应于相同 'Country' 的元素。
最好通过下面我打算做的示例来形象化它:
有没有办法自动生成这些分组框以达到预期的效果?
解决方案的关键是对数据进行分组,在这种情况下,我使用字典,其中每个键都是国家,其值是日期和行元组的列表。
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()
我有以下自动生成的列表,我想用它们用 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']
我知道我可以使用以下代码得到这种看起来像 table 的 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,当第一个列表中的 'Country' 国家连续出现时,它会合并对应于相同 'Country' 的元素。
最好通过下面我打算做的示例来形象化它:
有没有办法自动生成这些分组框以达到预期的效果?
解决方案的关键是对数据进行分组,在这种情况下,我使用字典,其中每个键都是国家,其值是日期和行元组的列表。
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()