简体   繁体   English

如何在pyside2 window中显示Python字典数据? [等候接听]

[英]How to show Python dictionary data in pyside2 window? [on hold]

I have a list of Dictionaries in python and I want to display those dictionaries data in python pyside2 window in the way that is shown in image attached.我在 python 中有一个字典列表,我想以附图所示的方式在 python pyside2 window 中显示这些字典数据。

link of image图片链接

These are three dictionaries with multiple keys and values, below is example dictionary这是三个具有多个键和值的字典,下面是示例字典

data = {
    "start time": ["2001-04-27t03:20:15-07:00","2001-04-27T12:20:15+02:00",
    "2001-04-27T10:20:15Z"],
    "Approximate datasets size in the file system" : ["6.940 MB","11 datasets"],
    "Datatypes" : ["APS", "Order APS","Slow Quantity", "Tacho Edges" , "Throughput"],
    "Quantities" : ["Rotational Speed" , "Sound Pressure"]   }

There is a list which contain 1000's of this kind of dictionaries and My Problem is how to display them in QT pyside2 the same way as shown in image attached.有一个包含 1000 种此类词典的列表,我的问题是如何在 QT pyside2 中显示它们,如附图所示。

It might look something like this:它可能看起来像这样:

import sys
from PyQt5 import QtWidgets, QtCore

class MyWindow(QtWidgets.QMainWindow): 
    def __init__(self, data):
        super().__init__()

        self.data = data

        self.scrollArea = QtWidgets.QScrollArea()
        self.scrollArea.setWidgetResizable(True)
        self.setCentralWidget(self.scrollArea)
        self.widget = QtWidgets.QWidget()             
        self.scrollArea.setWidget(self.widget)         

        button = QtWidgets.QPushButton("Click me")        
        button.clicked.connect(self.onButton)

        self.grid = QtWidgets.QGridLayout(self.widget)
        self.grid.addWidget(button) 

    def onButton(self):
        i = 1
        for item in self.data:
            textLeft = f'''
Start time:  
{item["start time"][0]} 
America/Los_Angeles
{item["start time"][1]} Original 
{item["start time"][2]} UTC

Approximate datasets size in the file system:   
{item["Approximate datasets size in the file system"][0]}      
            '''

            textRight = f'''
{item["Approximate datasets size in the file system"][1]}      
Datatypes:
{' '.join(item["Datatypes"][:3])}
{' '.join(item["Datatypes"][3:])}

Quantities:
{' '.join(item["Quantities"])}
            '''

            labelLeft = QtWidgets.QLabel()
            labelLeft.setText(textLeft)
            self.grid.addWidget(labelLeft, i, 0)

            labelRight = QtWidgets.QLabel()
            labelRight.setText(textRight)
            self.grid.addWidget(labelRight, i, 1)            

            line = QtWidgets.QFrame()
            line.setFrameShape(QtWidgets.QFrame.HLine)
            self.grid.addWidget(line, i+1, 0, 1, 2)
            i += 2


data = [
    {"start time": ["2001-04-27t03:20:15-07:00", "2001-04-27T12:20:15+02:00", "2001-04-27T10:20:15Z"],
    "Approximate datasets size in the file system" : ["6.940 MB","11 datasets"],
    "Datatypes" : ["APS", "Order APS","Slow Quantity", "Tacho Edges" , "Throughput"],
    "Quantities" : ["Rotational Speed" , "Sound Pressure"]},

    {"start time": ["2222-07:00", "2222:15+02:00", "2222:15Z"],
    "Approximate datasets size in the file system" : ["2.22 MB","11 datasets"],
    "Datatypes" : ["APS", "Order APS","Slow Quantity", "Tacho Edges" , "Throughput"],
    "Quantities" : ["Rotational Speed" , "Sound Pressure"]},

    {"start time": ["333-07:00", "333:15+02:00", "333:15Z"],
    "Approximate datasets size in the file system" : ["333 MB","11 datasets"],
    "Datatypes" : ["APS", "Order APS","Slow Quantity", "Tacho Edges" , "Throughput"],
    "Quantities" : ["Rotational Speed" , "Sound Pressure"] },
]  

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    myWindow = MyWindow(data)
    myWindow.resize(500, 300)
    myWindow.show()
    app.exec_() 

在此处输入图像描述

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

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