[英]PyQt “Not responding”
我将Selenium与Python结合使用。 单击按钮时,我在寡妇Python中收到一条消息:
####文件:曲在寡妇Python中不响应
我有以下脚本:
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
###文件: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_())
文件: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
实际上,您的主GUI根本不会冻结,而只是在执行log
并将控制权返回给主GUI时冻结,因为您没有在应用程序中实现任何类型的线程机制。
因此,作为解决方案,您需要使用threading
模块使用线程 log
方法来不阻塞主GUI,以下是一种通用方法,您需要阅读有关threading
更多信息:
1-在您的qu.py
文件中import threading
2-也在qu.py
定义此方法:
def launch_Selenium_Thread(self):
t = threading.Thread(target=self.log)
t.start()
3- connect
pushButton
的connect
方法更改为:
QtCore.QObject.connect(self.pushButton,QtCore.SIGNAL(_fromUtf8("clicked()")), self.launch_Selenium_Thread)
4-在qf.py
log3
方法中添加txt
参数:
def log3(self, txt):
text =unicode(txt)
print text
5-最后修复qu.py
log2
方法:
def log2(self):
from qf import functon
n=functon()
txt = self.lineEdit.text()
n.log3(txt)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.