Python Forum
PyQt5: Add Variable Number of Rows to Layout Based on Python Dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyQt5: Add Variable Number of Rows to Layout Based on Python Dictionary
#1
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!
Reply
#2
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.
Reply
#3
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_())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PyQt5 Relative Layout Unkovic 1 629 Nov-10-2023, 01:52 PM
Last Post: Axel_Erfurt
  [PyGUI] How to reuse a window layout based on mysql rowcount? retroisbest 2 2,464 Nov-09-2021, 09:02 AM
Last Post: retroisbest
Question Python 3.10 and PyQt5(6) zazen000 2 7,710 Jun-14-2021, 05:02 PM
Last Post: deanhystad
  Updating button text based upon different variable values knoxvilles_joker 0 2,241 Apr-18-2021, 04:13 AM
Last Post: knoxvilles_joker
  pyqt5 layout Nickd12 8 3,506 Jan-18-2021, 09:09 AM
Last Post: Axel_Erfurt
  [PyQt] Control a combobox from another python file PyQt5 prath 0 2,286 May-05-2020, 03:22 PM
Last Post: prath
  PyQt5: Iterate through rows to check for a value keegan_010 2 1,755 Mar-23-2020, 11:30 PM
Last Post: keegan_010
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 13,786 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  PyQt5: How do you set the user input of a line edit to a specific variable? YoshikageKira 17 11,684 Dec-26-2019, 03:18 PM
Last Post: Denni
  Find active PyQT5 textline and add result to dictionary mart79 3 2,229 Jul-31-2019, 03:25 PM
Last Post: Denni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020