0

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)
user3419537
  • 4,740
  • 2
  • 24
  • 42
Rajnine
  • 3
  • 3
  • What do you mean when you say you have a separate script that calls this? A "call" to this code will only define the class, not instantiate it. In its current form the code will not even show the button you want to click – user3419537 Feb 15 '18 at 12:21
  • the code shows the button and input box but does not do anything when clicked. What I meant by that is i have scriptA and this code is scriptB in a method within scriptA i say scrapeEmails.Window() to call the class.. is this correct? or should i make it just a function call instead of putting it in classes in scriptB? – Rajnine Feb 15 '18 at 14:34
  • @user3419537 comment above sorry forgot to tag you – Rajnine Feb 15 '18 at 14:35

2 Answers2

1

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).

MalloyDelacroix
  • 2,193
  • 1
  • 13
  • 17
  • thank you very much this fixed my problem and you provided me with a good, easy to understand explanation. I appreciate it – Rajnine Feb 15 '18 at 15:08
0

In my case I had accidently turn Enabled property of mainWindow to false and Nothing was working for hours. Just needed to check the enabled box of mainWindow

abdullah
  • 86
  • 1
  • 7