gpt4 book ai didi

python - PyQt “Not responding”

转载 作者:太空宇宙 更新时间:2023-11-04 03:22:03 52 4
gpt4 key购买 nike

我将 Selenium 与 Python 结合使用。单击按钮时,我在 widows Python 中收到一条消息:

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

####文件:曲
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()
###文件: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

“Not responding” in Python.

最佳答案

实际上,您的主 GUI 根本没有被卡住,只是在执行 log 并将控制权返回给主 GUI 时卡住,因为您没有在应用程序中实现任何类型的线程机制。

所以作为一个解决方案,你需要thread log方法来不阻塞你的主GUI,使用threading模块,下面是一个通用方式,您需要阅读更多关于 threading 的信息:

1 - 在您的 qu.py 文件中导入线程

2 - 在 qu.py 中定义此方法:

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

3 - 将 pushButtonconnect 方法更改为:

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

4 - 在qf.pylog3方法中添加,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)

关于python - PyQt “Not responding”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34386561/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com