Python Forum
Need some advice when to use self
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some advice when to use self
#1
Hello,
I need some advice on how to use "self" correctly.

I want to write an app that consists of a main window "MainWindow" and a few additional windows - I've also added "AdjustmentsWindow" and "ExportPdfWindow" next to "MainWindow" as an example.

I've read that self plays a role in the process of object creation (instantiation) so that it can be assigned which object it is.

For example, I create only one object from the "MainWindow" class (w = MainWindow()).

Do I always have to use self in "def __init__(self): " of "MainWindow"?

Unfortunately, I'm still a bit unsure where to use self...

It would be great if someone could give me some hints on how to use self...

Thank you very much!!

import sys
from pathlib import Path
from PyQt6.QtWidgets import (
QApplication,
QLabel,
QMainWindow,
QPushButton,
QHBoxLayout,
QVBoxLayout,
QWidget,
QGroupBox,
QListWidget,
QCheckBox,
QRadioButton,
)

class ExportPdfWindow(QWidget):
    def __init__(self):
        super().__init__()
        label = QLabel("export pdf(s)")
        label2 = QLabel("what would you like to export from the pdf?")
        label3 = QLabel("which pages would you like to receive?")
        radiobutton = QRadioButton("specific page(s)")
        radiobutton2 = QRadioButton("specific page range(s)")
        radiobutton3 = QRadioButton("all pages individually")
        button = QPushButton("apply selection")
        layout = QVBoxLayout()
        for t in [label, label2, label3, radiobutton, radiobutton2, radiobutton3, button]:
            layout.addWidget(t)
        self.setLayout(layout)


class AdjustmentsWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("abc")
        self.setGeometry(300, 300, 300, 300)
        label = QLabel("adjustments")
        listbox1 = QListWidget()
        button_converted_files = QPushButton("converted files")
        listbox2 = QListWidget()
        button_created_files = QPushButton("created files")
        button_donate = QPushButton("donate")
        layout = QVBoxLayout()
        for k in [label, listbox1, button_converted_files, listbox2, button_created_files, button_donate]:
            layout.addWidget(k)
        self.setLayout(layout)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        qr = self.frameGeometry()
        cp = self.screen().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

        self.w = QWidget()
        self.setCentralWidget(QWidget())
        self.setWindowTitle("abc")

        self.groupbox_have = QGroupBox("what do you have?")
        groupbox_have_layout = QVBoxLayout()
        self.groupbox_have.font().setBold(True)
        self.listbox_medium = QListWidget()
        self.listbox_medium2 = QListWidget()
        self.checkbox = QCheckBox("stack contains patchTpages")
        for x in [self.listbox_medium, self.listbox_medium2, self.checkbox]:
            groupbox_have_layout.addWidget(x)
        self.groupbox_have.setLayout(groupbox_have_layout)

        self.groupbox_receive = QGroupBox("what would you like to receive?")
        groupbox_receive_layout = QVBoxLayout()
        self.groupbox_receive.font().setBold(True)
        self.listbox_target = QListWidget()
        self.groupbox_move = QGroupBox("move file(s) to")
        self.groupbox_move.font().setBold(True)
        checkbox_move_to = QCheckBox("move file(s) to desired folder(s)")
        groupbox_move_layout = QVBoxLayout()
        groupbox_move_layout.addWidget(checkbox_move_to)
        self.groupbox_move.setLayout(groupbox_move_layout)

        groupbox_receive_layout = QVBoxLayout()
        for y in [self.listbox_target, self.groupbox_move]:
            groupbox_receive_layout.addWidget(y)
        self.groupbox_receive.setLayout(groupbox_receive_layout)

        self.groupbox_profile = QGroupBox("which profile should be used?")
        self.groupbox_profile.font().setBold(True)
        self.listbox_profile = QListWidget()
        self.button_add_profile = QPushButton("add profile")
        self.button_clear_profile = QPushButton("clear profile")

        groupbox_profile_intern_layout = QHBoxLayout()
        for x in [self.button_add_profile, self.button_clear_profile]:
            groupbox_profile_intern_layout.addWidget(x)

        groupbox_profile_layout = QVBoxLayout()
        groupbox_profile_layout.addWidget(self.listbox_profile)
        groupbox_profile_layout.addLayout(groupbox_profile_intern_layout)
        self.groupbox_profile.setLayout(groupbox_profile_layout)

        self.button_apply_selection = QPushButton("apply selection")
        self.button_adjustments = QPushButton("adjustments")
        self.label = QLabel("abc")

        layoutBase = QVBoxLayout()
        for c in [self.label, self.groupbox_have, self.groupbox_receive, self.groupbox_profile, self.button_apply_selection, self.button_adjustments]:
            layoutBase.addWidget(c)
        self.w.setLayout(layoutBase)
        self.setCentralWidget(self.w)

        # fill listbox
        self.listbox_medium.insertItem(0, "paper")
        self.listbox_medium.insertItem(1, "file(s)")

        self.button_add_profile.clicked.connect(self.button_add_profile_clicked)
        self.button_clear_profile.clicked.connect(self.button_clear_profile_clicked)
        self.button_apply_selection.clicked.connect(self.button_apply_selection_clicked)
        self.button_adjustments.clicked.connect(self.button_adjustments_clicked)

    def button_add_profile_clicked(self):
        print("add_profile")

    def button_clear_profile_clicked(self):
        print("clear_profile")

    def button_adjustments_clicked(self):
        self.w = AdjustmentsWindow()
        self.w.show()

    def button_apply_selection_clicked(self):
        print("apply_selection")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyleSheet(Path("stylesheet.qss").read_text())
    w = MainWindow()
    w.show()
    app.exec()
Reply


Messages In This Thread
Need some advice when to use self - by flash77 - Jul-21-2023, 07:21 PM
RE: Need some advice when to use self - by flash77 - Jul-22-2023, 04:04 AM

Forum Jump:

User Panel Messages

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