Skip to content Skip to sidebar Skip to footer

Simplify My Code, One Function To Create Is Used By Other Functions

I have this code: class Main(QWidget): def __init__(self): super().__init__() self.init_gui() def init_gui(self): self.layout_main = QVBoxLayout()

Solution 1:

Try it:

classMain(QWidget):
    def__init__(self):
        super().__init__()
        self.init_gui()

    definit_gui(self):
        self.layout_main = QVBoxLayout()
        self.setLayout(self.layout_main)

        self.scroll_areas()                                          # +++

        self.first()
        self.second()

        self.showMaximized()

    defscroll_areas(self):
        scroll_area = QScrollArea(self)
        widget = QWidget()
        self.layout = QVBoxLayout()

        scroll_area.setWidgetResizable(True)
        scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        scroll_area.setFixedSize(200, 200)
        widget.setLayout(self.layout)
        scroll_area.setWidget(widget)

        # self.layout_main.addLayout(layout)
        self.layout_main.addWidget(scroll_area)                       # +++deffirst(self):
        title = QLabel("<h1>First</h1>")
        title.setTextFormat(Qt.RichText)
        self.layout.addWidget(title)                                  # +++defsecond(self):
        title = QLabel("<h1>Second</h1>")
        title.setTextFormat(Qt.RichText)
        self.layout.addWidget(title)                                  # +++

enter image description here

Post a Comment for "Simplify My Code, One Function To Create Is Used By Other Functions"