2021年3月9日星期二

How can I design functions to make the buttons work as I hope in pyqt?

Suppose I have a file named file.json, and the structure of this file is as follows:

{      "0":{          "n1":{              "value": "start",              "state": "True",          },          "n2":{              "value": "null",              "state": "True",          },          "n3":{              "value": "null",              "state": "True",          },      },      "1":{          "n1":{              "value": "start",              "state": "False",          },          "n2":{              "value": "finished",              "state": "True",          },          "n3":{              "value": "finished",              "state": "True",          },      }      "1":{          "n1":{              "value": "finished",              "state": "False",          },          "n2":{              "value": "finished",              "state": "False",          },          "n3":{              "value": "finished",              "state": "False",          },      }  }  

The keys 0, 1, 2 are orders to be shown in a QStandardItemModel object. The keys n1, n2 n3 are nodes in a graph. "value", "state" are items. At present, I've already had the JSON data and the data is graphData. My purpose is to show the data by clicking a button.
At first, after I click button3 in the function setButton, the graphData[0] should be shown in the following table:
| value | state | | -------- | ------ | | start | true | | null | true | | null | true | Then I click button3 again, the table should show graphData[1] as before until all the data was processed.
The source code is as follows:

import sys  from PyQt5.QtWidgets import QPushButton, QWidget, QApplication, QVBoxLayout, QHBoxLayout, QTableView, QHeaderView, QDesktopWidget, QMainWindow, QTableWidget, QTableWidgetItem  from PyQt5.QtGui import QIcon, QStandardItemModel, QStandardItem  from PyQt5 import QtGui  from PyQt5.QtCore import pyqtSignal, QObject  from functools import partial    import json    jsonname = "kkk"    class UserInterface(QWidget):      def __init__(self):          super().__init__()          self.initUI()          def setFrame(self):          # frame          self.setGeometry(900, 900, 900, 440)          self.setWindowTitle('title')          self.setWindowIcon(QIcon('web.png'))  

These two functions works fine, you can ignore them for a while.

    def buttonClicked(self, button):          # This function get a file name by           # clicking different buttons to get different files.          # It works but not works as I hope.           if button == self.button2:              jsonname = "loop.json"          else:              jsonname = "noloop.json"          self.content(jsonname)  

Function buttonClicked is set to click different buttons to select different files.

    def setContentLayout(self, jsonname):          # show data ant set Layout            titles = ['value', 'state']          titlecount = len(titles)           rowcount = 2           contentLayout = QHBoxLayout()          self.model = QStandardItemModel(rowcount, titlecount)            # click to show new data          self.setButtonNext(titles, jsonname, contentLayout)            if jsonname != "kkk":              print("start")              with open(jsonname, 'r', encoding='utf8')as fp:                  graphData = json.load(fp) # json to dict              epochs = len(graphData)  

Function setContentLayout is what I need to implement.

    def content(self, jsonname):          # add the contentLayout to the frame          titles, contentLayout = self.setContentLayout(jsonname)          self.model.setHorizontalHeaderLabels(titles)          tableView = QTableView()          tableView.horizontalHeader().setStretchLastSection(True)          tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)          tableView.verticalHeader().setStretchLastSection(True)          tableView.verticalHeader().setSectionResizeMode(QHeaderView.Stretch)          tableView.setModel(self.model)          contentLayout.addWidget(tableView)            return contentLayout        def setButton(self):          # set and add buttons          # button1 and button2 work but not very well          self.button1 = QPushButton('noloop', self)          self.button1.setToolTip('click to get noloop data')          self.button1.resize(self.button1.sizeHint())          self.button2 = QPushButton('loop', self)          self.button2.setToolTip('click to get loop data')          self.button2.resize(self.button2.sizeHint())            self.button1.clicked.connect(partial(self.buttonClicked, self.button1))          self.button2.clicked.connect(partial(self.buttonClicked, self.button2))            self.button3 = QPushButton('next', self)          self.button3.setToolTip('click to next')          self.button3.resize(self.button3.sizeHint())          self.button3.clicked.connect(self.setButtonNext)          buttons = [self.button1, self.button2, self.button3]          buttonbox = QHBoxLayout()          for bt in buttons:              buttonbox.addWidget(bt)          buttonbox.addStretch()            return buttonbox        def initUI(self):          vbox = QVBoxLayout()          self.setFrame()          buttonbox = self.setButton()          contentLayout = self.content(jsonname)            vbox.addLayout(buttonbox)          vbox.addLayout(contentLayout)          self.setLayout(vbox)            self.center()          self.show()  

These functions maybe work fine, you can ignore them for a while.

The problem is I don't know how to implement the setContentLayout. How would I go about doing this?

https://stackoverflow.com/questions/66557779/how-can-i-design-functions-to-make-the-buttons-work-as-i-hope-in-pyqt March 10, 2021 at 11:06AM

没有评论:

发表评论