简体   繁体   中英

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.

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.

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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