Making a Dropdown List of Selectable Subjects #191
-
Hello everyone! I was looking into the GUI widgets some more for examples as I'm trying to learn some basic GUI design/programming. I've gotten kinda stuck understanding how this piece works. Namely, how to use the I have a directory with a bunch of subjects in it. For our microscope stuff, it would be neat to have a window pop-up when the program started that lets you make a "flight manifest" of the subjects you plan on running that day. Since you can only run one mouse at a time, the list of subjects you select would just be a list that the program iterates through. Getting a list of a directories' contents and then making those clickable objects is where I seem to have gotten stuck. Any advice for how to do that part? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
so you want to
Typically the UI element you would use for multiple-selection from a list is a list of checkboxes, see an example here: https://github.com/pyside/pyside2-examples/blob/696f0677ea3c982344364f633845ed39b686cdb3/examples/widgets/widgets/groupbox.py So you'd populate a layout with pairs of (QCheckBox, QLabel)s that let you check individual subjects, and then read the checked values when you want to use them. So a quick example. from PySide2 import QtWidgets
from PySide2.QtWidgets import QCheckBox, QPushButton, QLabel, QGroupBox
from typing import List, Dict, Tuple, Optional
class CheckList(QtWidgets.QWidget):
def __init__(self, subjects:List[str], title:str="Subjects", **kwargs):
super(CheckList, self).__init__(**kwargs)
self._checkboxes = {} # type: Dict[str, QCheckBox]
self.title = title
self.subjects = subjects
self.layout = QtWidgets.QVBoxLayout()
# Add checkboxes to layout
self.layout.addWidget(self._init_checkboxes())
# Add button to layout
self.ok_button = QPushButton("OK")
self.layout.addWidget(self.ok_button)
# connect button to action triggered when clicked
self.ok_button.clicked.connect(self.print_values)
# set layout of main widget
self.setLayout(self.layout)
def value(self) -> Dict[str, bool]:
return {
sub: box.isChecked() for sub, box in self._checkboxes.items()
}
def _init_checkboxes(self) -> QGroupBox:
# Button groupbox
check_group = QGroupBox(self.title)
# button layout
check_layout = QtWidgets.QVBoxLayout()
# make checkboxes
for subject in self.subjects:
box = QCheckBox(subject)
# add checkbox to layout so it's displayed
check_layout.addWidget(box)
# save reference to checkboxes to get value later
self._checkboxes[subject] = box
# set layout full of buttons for groupbox
check_group.setLayout(check_layout)
return check_group
def print_values(self):
print(self.value())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
subject = CheckList(subjects=[
'subject 1', 'subject 2', 'subject 3']
)
subject.show()
sys.exit(app.exec_()) and does this when you click 'ok' So you don't want to build all the logic into one object, for example this one takes a generic list of strings, so you would want to do the loading of the list of subjects elsewhere (idk, like if they're files you'd just do Notice the different parts of that example: take in a list of strings, create GUI objects for the input, create a button, create a method for getting the value of the checkboxes, and then make a method that fires when the button is clicked. If i was intending on using this code i'd also put all the gui creation stuff in its own method rather than in Don't overthink inheritance. It's as simple as class ClassA:
def class_a_method(self):
print("I am a method defined in class A")
class ClassB(ClassA):
def class_b_method(self):
print("I am a method defined in class B")
class ClassC(ClassB):
def class_a_method(self):
print("I override class A's method in class C")
which works like this: Classes that inherit from another class start out as "copies" of that class, and then allow you to extend or replace different methods or attributes. So you might want to have a generic |
Beta Was this translation helpful? Give feedback.
-
I discovered that we have many subjects to image. It's so long that the checkboxes no longer fit on the screen! So I've been looking into how to add a scrollbar to things. Here's how I've gotten at least a basic scrollbar going:
The main changes I made were to change the This works for some reason. When you close the window, it saves the selected subjects. I'm not really sure why. The main problem now is I can't seem to retain the EDIT: Moving around the button and assigning it to the scrollbar widget populated by
|
Beta Was this translation helpful? Give feedback.
so you want to
Typically the UI element you would use for multiple-selection from a list is a list of checkboxes, see an example here: https://github.com/pyside/pyside2-examples/blob/696f0677ea3c982344364f633845ed39b686cdb3/examples/widgets/widgets/groupbox.py
So you'd populate a layout with pairs of (QCheckBox, QLabel)s that let you check individual subjects, and then read the checked values when you want to use them. So a quick example.