Skip to content
Advertisement

clicked.connect() will not access the method

I have a seperate script that calls this one… I don’t understand why my button click is not going into the corresponding function. I am quite new at this so if you do know the answer, could you explain it to me so that I can learn it.

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class Window (QWidget):

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

    def initUI(self):
        self.urlEdit = QLineEdit()
        self.windowWid = QWidget()

        urlLabel = QLabel("Enter website to scrape:")
        self.urlEdit.setFixedHeight(30)
        self.urlEdit.setFixedWidth(200)
        layout = QGridLayout(self.windowWid)
        layout.addWidget(urlLabel, 0, 0)
        layout.addWidget(self.urlEdit, 0, 1)
        done = QPushButton("Done",self)

        done.setFixedWidth(60)
        layout.addWidget(done,2,3)
        layout.setRowStretch(4, 1)
        layout.setColumnStretch(4,1)
        done.clicked.connect(self.grabtext)

    def grabtext(self):
        print("CANT REACH HERE")
        answer = self.urlEdit.text()
        print(answer)

Advertisement

Answer

Remove this line: self.windowWid = QWidget() and change layout = QGridLayout(self.windowWid) to layout = QGridLayout(self)

Basically what is happening is when you call to show this widget, with the line self.windowWid = QWidget() you are initializing a blank QWidget and then assigning your layout and other elements to this widget. You want to assign these elements to the instance of the Window widget instead, which is why you use layout = QGridLayout(self).

Advertisement