Ahmad Ali
Ahmad Ali

Reputation: 57

PyQt “Not responding”

I'm using Selenium with Python. I'm getting a message in widows Python, when the button is clicked:

Not responding in widows Python Not responding in widows Python I have the following script:

####file:qu
from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s
#####
try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(340, 110, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(324, 200, 111, 23))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.lineEdit = QtGui.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(390, 240, 151, 20))
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        QtCore.QObject.connect(self.pushButton,QtCore.SIGNAL(_fromUtf8("clicked()")), self.log)
        QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), self.log2)


    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.pushButton.setText(_translate("MainWindow", "PushButton", None))
        self.pushButton_2.setText(_translate("MainWindow", "PushButton_2", None))
    def log(self):############################
            from qf import functon 
            n=functon()
            n.log1()
    def log2(self):
            from qf import functon 
            n=functon()
            n.log3() 
###file:qm
# -*- coding: utf-8 -*-

from PyQt4 import QtGui
from PyQt4.QtGui import QApplication
from PyQt4 import QtCore, QtGui
import sys
from qu import Ui_MainWindow


class MainWindow(QtGui.QMainWindow,Ui_MainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MainWindow()
    myapp.show()
    sys.exit(app.exec_())
file:qf
from   selenium import webdriver
from PyQt4 import QtCore, QtGui
import sys
from qu import Ui_MainWindow


class functon ():
    def __init__(self, parent=None):

        self.parent=parent


    def log1(self):
            browser =webdriver.Firefox()
            browser.get( "http://google.com" )

    def log3(self):        
            text =unicode(self.lineEdit.text())
            print text

“Not responding” in Python.

Upvotes: 2

Views: 6690

Answers (1)

Iron Fist
Iron Fist

Reputation: 10951

Actually, You main GUI is not frozen at all but only for the time log gets executed and returns control to the main GUI, as you are not implementing any sort of threading mechanism in your application.

So as a solution, you need to thread log method to not block your main GUI, using threading module, the following is a generic way, you need to read more about threading:

1 - import threading in your qu.py file

2 - Define this method in qu.py as well:

def launch_Selenium_Thread(self):
        t = threading.Thread(target=self.log)
        t.start()

3 - Change the connect method of pushButton to:

QtCore.QObject.connect(self.pushButton,QtCore.SIGNAL(_fromUtf8("clicked()")), self.launch_Selenium_Thread)

4 -Add in qf.py log3 method, txt parameter:

def log3(self, txt):        
        text =unicode(txt)
        print text

5 - Finally fix in qu.py log2 method:

def log2(self):
            from qf import functon 
            n=functon()
            txt = self.lineEdit.text()
            n.log3(txt) 

Upvotes: 5

Related Questions