1
\$\begingroup\$

I will import given code for my main QMainWindow.(I only add import sys etc. to run it) Is there a more compact way to code this lines. Output is correct for my expectation.

from PyQt5.QtWidgets import *
import sys


class ButtonWidget(QWidget):

    def __init__(self):
        super(ButtonWidget, self).__init__()

        # Function
        button1 = QRadioButton("Sinus")
        button2 = QRadioButton("Cosines")
        # Color
        button3 = QRadioButton("Red")
        button4 = QRadioButton("Green")
        # Line
        button5 = QRadioButton("Solid")
        button6 = QRadioButton("Dashed")
        # Left Group
        left_group = QGroupBox("Left Group")
        left_group_layout = QVBoxLayout()
        left_group_layout.addWidget(button1)
        left_group_layout.addWidget(button2)
        left_group.setLayout(left_group_layout)
        # Middle Group
        middle_group = QGroupBox("Middle Group")
        middle_group_layout = QVBoxLayout()
        middle_group_layout.addWidget(button3)
        middle_group_layout.addWidget(button4)
        middle_group.setLayout(middle_group_layout)
        # Right Group
        right_group = QGroupBox("Right Group")
        right_group_layout = QVBoxLayout()
        right_group_layout.addWidget(button5)
        right_group_layout.addWidget(button6)
        right_group.setLayout(right_group_layout)
        # Main Group
        main_group = QGroupBox("Main Group")
        main_group_layout = QHBoxLayout()
        main_group_layout.addWidget(left_group)
        main_group_layout.addWidget(middle_group)
        main_group_layout.addWidget(right_group)
        main_group.setLayout(main_group_layout)
        # Widget
        main_widget = QWidget()
        main_widget_layout = QVBoxLayout()
        main_widget.setLayout(main_widget_layout)
        main_widget_layout.addWidget(main_group)
        # Layout Set
        self.setLayout(main_widget_layout)

        self.show()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ui = ButtonWidget()
    sys.exit(app.exec_())
\$\endgroup\$
1
\$\begingroup\$

Yes there is a solution. The obvious solution is of course to use loops. Here is my try using a composite dictionary. It is functionally equivalent to your code and will save you about a dozen lines but hopefully adds flexibility. This was a quick job, so maybe you can take it further.

from PyQt5.QtWidgets import *
import sys


class ButtonWidget(QWidget):

    def __init__(self):
        super(ButtonWidget, self).__init__()

        groups = {"Left Group": ("Sinus", "Cosines"),
            "Middle Group": ("Red", "Green"),
            "Right Group": ("Solid", "Dashed")
        }

        # Main Group
        main_group = QGroupBox("Main Group")
        main_group_layout = QHBoxLayout()

        # loop on group names
        for group, buttons in groups.items():
            group_box = QGroupBox(group)
            group_layout = QVBoxLayout()
            for button_text in buttons:
                group_layout.addWidget(QRadioButton(button_text))

            group_box.setLayout(group_layout)
            main_group_layout.addWidget(group_box)

        main_group.setLayout(main_group_layout)

        # Widget
        main_widget = QWidget()
        main_widget_layout = QVBoxLayout()
        main_widget.setLayout(main_widget_layout)
        main_widget_layout.addWidget(main_group)
        # Layout Set
        self.setLayout(main_widget_layout)

        self.show()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ui = ButtonWidget()
    sys.exit(app.exec_())

NB: you'll probably have to add names to the generated controls (the buttons at least).

PS: personally I use the QT designer to build forms unless I need a highly dynamic layout. Adding controls in code is tedious and less visual.

\$\endgroup\$
1
  • \$\begingroup\$ Actually, I'm trying to create engineering calculation GUI for my job. I think, I can create basic one. However I also want to improve myself. I will use numpy etc. What do you suggest for not highly dynamic layout \$\endgroup\$ – Gokberk May 25 '20 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.