问题描述
我有一个非常简单的程序,用Python编写,并使用PyQt5的GUI进行监视,该程序监视是否有Internet连接。只要存在互联网连接(“ CONECTADO”)和其对应对象(“ NO CONECTADO”)(未连接),该应用程序就会以卡斯蒂利亚西班牙语显示一条消息。每秒,“ CONECTADO”一词通过将其前景从深绿色更改为浅绿色来“点亮”,“ NO CONECTADO”消息也将变为红色,使其消失并每秒钟返回一次。我可以设法获得那种效果,但是只能持续十秒钟左右,因为所谓的无限循环并非如此。在那之后,无论我是否断开与网络的连接,消息均保持当前状态。就像线程已经冻结或停止运行一样,我真的不知道。
这是我的代码:
from PyQt5 import QtCore,QtGui,QtWidgets
import urllib.request
import time
import threading
from threading import Lock
class Ui_MainWindow(object):
def setupUi(self,MainWindow):
MainWindow.setobjectName("MainWindow")
MainWindow.resize(800,600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setobjectName("centralwidget")
self.lbl_estado_conex = QtWidgets.QLabel(self.centralwidget)
self.lbl_estado_conex.setGeometry(QtCore.QRect(170,190,481,91))
self.lbl_estado_conex.setStyleSheet("background:black;color:white;font-size:50px;border-radius:20px;")
self.lbl_estado_conex.setAlignment(QtCore.Qt.AlignCenter)
self.lbl_estado_conex.setobjectName("lbl_estado_conex")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setobjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.estilo_conectado="background:black;color:green;font-size:50px;border-radius:20px;"
self.estilo_luminoso="background:black;color:lightgreen;font-size:50px;border-radius:20px;"
self.estilo_no_conectado="background:black;color:red;font-size:50px;border-radius:20px;"
def actualizar_label(self):
self.threadclass=ThreadClass()
self.threadclass.start()
def retranslateUi(self,MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setwindowTitle(_translate("MainWindow","Monitor De Conexión LAN"))
self.lbl_estado_conex.setText(_translate("MainWindow","Estado"))
class ThreadClass(QtCore.QThread):
def __init__(self,parent=None):
super(ThreadClass,self).__init__(parent)
def run(self):
self.host='http://192.168.1.1'
while True:
try:
urllib.request.urlopen(self.host) #Python 3.x
self.mensaje="CONECTADO"
ui.lbl_estado_conex.setText(self.mensaje)
ui.lbl_estado_conex.setStyleSheet(ui.estilo_conectado)
time.sleep(1)
ui.lbl_estado_conex.setStyleSheet(ui.estilo_luminoso)
time.sleep(1)
except:
self.mensaje="NO CONECTADO"
ui.lbl_estado_conex.setText(self.mensaje)
ui.lbl_estado_conex.setStyleSheet(ui.estilo_no_conectado)
time.sleep(1)
ui.lbl_estado_conex.setText("")
if __name__ == "__main__":
import sys
bloqueo=Lock()
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
ui.actualizar_label()
sys.exit(app.exec_())
我已经阅读并观看了有关该主题的视频(Python中的Qt线程),但没有一个可以清除任何内容。这实际上就是编写这样一个简单的程序的要点:不仅要用Python,而且要用其他语言完全了解线程。由于我使用的是PyQt5,因此此处和其他任何地方发布的PyQt4代码段均对我不起作用。请帮助。
非常感谢您。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)