How I can run while loop with PyQt5(如何使用 PyQt5 运行 while 循环)
问题描述
我在一个项目上工作:程序下载但我在使用 while 循环时遇到问题,用于检查与 Internet 的连接,如果 true 不 setText('') to lable 和 if Flase setText('anyText') to lable
I work on one Project : Program download but I have a problem with while loop for check the connection with the internet and if true doesn't setText('') to lable and if Flase setText('anyText') to lable
连接检查方法
def checkInternetConnection(self,host="8.8.8.8", port=53, timeout=3):
while self.conection==False:
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
self.conection = True
return self.conection
except Exception as e:
print(e)
self.label_9.setText('Please Check Internect Connection')
self.conection = False
return self.conection
self.finished.emit()
我已经厌倦了 QThread .请问我该怎么做:)?如果连接丢失=False setText('check internet') 以及连接变为 true 时,应用程序正在运行时 setText('')
I have tired with QThread . Please How I can do it :) ? And when app is running if connection is lost=False setText('check internet') and when the connection become true setText('')
构造者
From_Class,_=loadUiType(os.path.join(os.path.dirname(__file__),'designe.ui'))
class mainApp(QMainWindow,From_Class):
finished = pyqtSignal()
def __init__(self,parent=None):
super(mainApp, self).__init__(parent)
QMainWindow.__init__(self)
super().setupUi(self)
self.handleGui()
self.handleButton()
self.setWindowIcon(QIcon('mainIcon.png'))
self.menuBarW()
self.conection = False
主代码
def main():
app = QApplication(sys.argv)
window = mainApp()
window.checkInternetConnection()
window.show()
app.exec()
if __name__=='__main__':
main()
推荐答案
QThread不要太复杂,使用线程库:
Do not get complicated with QThread, use the threading library:
def main():
app = QtWidgets.QApplication(sys.argv)
window = mainApp()
threading.Thread(target=window.checkInternetConnection, daemon=True).start()
window.show()
app.exec()
另一方面,由于您使用的是线程,因此不应从另一个线程更新 GUI,为此您可以使用 QMetaObject::invokeMethod
:
On the other hand, since you are using a thread, you should not update the GUI from another thread, for this you can use QMetaObject::invokeMethod
:
def checkInternetConnection(self,host="8.8.8.8", port=53, timeout=3):
while True:
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
self.conection = True
except Exception as e:
self.conection = False
print(e)
msg = "" if self.conection else 'Please Check Internect Connection'
print("msg", msg)
QtCore.QMetaObject.invokeMethod(self.label_9, "setText",
QtCore.Qt.QueuedConnection,
QtCore.Q_ARG(str, msg))
self.finished.emit()
这篇关于如何使用 PyQt5 运行 while 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 PyQt5 运行 while 循环
基础教程推荐
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01