Python Forum
PyQt5: Add Variable Number of Rows to Layout Based on Python Dictionary - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: PyQt5: Add Variable Number of Rows to Layout Based on Python Dictionary (/thread-7378.html)



PyQt5: Add Variable Number of Rows to Layout Based on Python Dictionary - kennybassett - Jan-07-2018

Sorry if the question was a bit vague! I am new to using PyQt (and making GUIs in general), and I am making a script that detects installed programs on my computer and retrieves the name of the program and its executable file path. So far I have a dictionary with file names as keys and the corresponding file paths as the key values.

Here is a snippet, though the actual dictionary is much longer:
{'Nexus Mod Manager': 'C:\\Games\\Nexus Mod Manager\\NexusClient.exe',
'CCleaner': 'E:\\Program Files\\CCleaner\\CCleaner.exe'}
My goal is to create a GUI that has a row for each key-value pair in the dictionary. I would also like to put 2 buttons (Yes and No) in each row to let the user choose if the file path is correct. If it is correct, it will execute some other function.

One problem that I'm facing is that the dictionary doesn't have one set length. On my computer, there are about 53 keys, but the number may be very different for another computer or if I install or uninstall programs in the future.

I prefer PyQt since that is the module in which I am most knowlegeable for creating GUIs, but I am open to other methods as well.

Thanks!


RE: PyQt5: Add Variable Number of Rows to Layout Based on Python Dictionary - seco - Oct-02-2018

Why don't using QtableWidget for display and store your data in a tuple or a list?
This tutorial explains many things about PyQt5: PyQt5 tutorial
Also, you can insert any widget inside the QTableWidget like buttons checkboxes as the tutorial described.
Hope that helps.


RE: PyQt5: Add Variable Number of Rows to Layout Based on Python Dictionary - Alfalfa - Oct-02-2018

This should get you started:
#!/usr/bin/python3
import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = QtWidgets.QWidget(self)
        self.setCentralWidget(self.ui)
        self.ui.tree = Tree()
        self.ui.button = QtWidgets.QPushButton("Validate")
        self.ui.button.clicked.connect(self.validate)
        self.ui.layout = QtWidgets.QVBoxLayout()
        self.ui.layout.addWidget(self.ui.tree)
        self.ui.layout.addWidget(self.ui.button)
        self.ui.setLayout(self.ui.layout)
        self.show()

    def validate(self):
        print("Checked:")
        for item in range(self.ui.tree.topLevelItemCount()):
            item = self.ui.tree.topLevelItem(item)
            if bool(item.checkState(0)):
                print(item.text(0), item.text(1))

class Tree(QtWidgets.QTreeWidget):
    def __init__(self):
        super().__init__()
        self.setHeaderLabels(["Name", "Path"])
        self.fill()

    def fill(self):
        apps = \
        {
            'Nexus Mod Manager': 'C:\\Games\\Nexus Mod Manager\\NexusClient.exe',
            'CCleaner': 'E:\\Program Files\\CCleaner\\CCleaner.exe'
        }
        for key, value in iter(apps.items()):
            item = QtWidgets.QTreeWidgetItem()
            item.setCheckState(0, QtCore.Qt.Unchecked)
            item.setText(0, key)
            item.setText(1, value)
            self.addTopLevelItem(item)


if __name__== '__main__':
    app = QtWidgets.QApplication([])
    gui = Main()
    sys.exit(app.exec_())