0

I've created a layout with PyQt and am trying to call a function that does something with the user input from a QLineEdit, having trouble understanding instances and all of the self got my head spinning, here's my code

    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):

        self.setWindowIcon(QtGui.QIcon("Comfy.png"))
        self.setWindowTitle("test")

        label = QLabel("Title: ")
        wp_edit = QLineEdit()

        city = QLabel("City: ")
        city_edit = QLineEdit()

        phone = QLabel("Phone: ")
        phone_edit = QLineEdit()

        owner = QLabel("Owner: ")
        owner_edit = QLineEdit()

        description = QLabel("Description: ")
        description_edit = QPlainTextEdit()

        logo = QLabel(self)
        logo.setAlignment(Qt.AlignCenter)
        pixmap = QPixmap("logo.png")
        smaller_pixmap = pixmap.scaled(296, 128, Qt.KeepAspectRatio)
        logo.setPixmap(smaller_pixmap)

        radio1 = QRadioButton("test")
        radio2 = QRadioButton("test")
        radio3 = QRadioButton("test")
        radio4 = QRadioButton("test")
        radio5 = QRadioButton("test")

        button = QPushButton("Create", self)
        button.setIcon(QtGui.QIcon("Check.png"))
        button.clicked.connect(self.create_post)

        h = QHBoxLayout()
        h.addWidget(label)
        h.addWidget(wp_edit)

        h1 = QHBoxLayout()
        h1.addWidget(city)
        h1.addWidget(city_edit)
        h1.addWidget(phone)
        h1.addWidget(phone_edit)

        h2 = QHBoxLayout()
        h2.addWidget(owner)
        h2.addWidget(owner_edit)

        h3 = QHBoxLayout()
        h3.addWidget(radio1)
        h3.addWidget(radio2)
        h3.addWidget(radio3)
        h3.addWidget(radio4)
        h3.addWidget(radio5)

        v = QVBoxLayout()
        v.addStretch(1)
        v.addWidget(logo)
        v.addLayout(h)
        v.addWidget(description)
        v.addWidget(description_edit)
        v.addLayout(h1)
        v.addLayout(h2)
        v.addLayout(h3)

        v.addWidget(button)

        self.setLayout(v)

        self.show()

    def create_post(self):
        print(self.wp_edit.text())

I've tried many things I found online, followed a few videos trying to understand but nothing seems to work, how do I use the user input from the QLineEdit in my create_post function?

1 Answers1

0

You need to use self.wp_edit everywhere:

    self.wp_edit = QLineEdit()
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • That worked! I do get a green line in pycharm under it saying it's outside the __init__ function, should I ignore it or should I cut the init_ui function and move all the variables I want to use the to __init__ function? – SirBoboHobo Mar 29 '20 at 16:13
  • My opinion is that you should ignore it. I think it is better to have complicated init in one place. It is a choice where it goes. – quamrana Mar 29 '20 at 16:18