PyQt - 在运行时创建QLabel并在之后更改文本

时间:2018-05-27 17:37:04

标签: python pyqt pyqt5 qlabel

我想在运行时动态创建QLabel,然后更改Text。 我这样做了:

def __init__(self, *args, **kwargs):
    QWidget.__init__(self, *args, **kwargs)
    self.counter = 0
    self.items = self.read_hosts()
    self.layout = QGridLayout()
    for item in self.items:
        self.item = QLabel(item, self)

        self.item.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.item.setAlignment(Qt.AlignCenter)
        self.item.setStyleSheet("QLabel {background-color: green;}")
        self.layout.addWidget(self.item,self.counter, 0)
        self.counter += 1
    self.setLayout(self.layout)
    self.startWorker()
    self.show()

def change_txt(self, lb, i):
     self.item.setText("{}".format(i))

它无效。 我明白为什么它会改变最后一个标签的文字。我在任务期间做错了什么。

如何完全可变地创建所有标签并随后更改文本?

我正在使用:

PyQT5 在Windows 10上

谢谢!

以下是我的全部代码:

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import os

class Worker(QObject):
    update = pyqtSignal(str,int)
    exception = pyqtSignal(str)

    def read_hosts(self):
        #try:
            file = open("hosts.ini", "r") 
            return file.readlines() 
        #except Exception as ex:
            #self.exception.emit(str(ex))

    def check_ping(self):
        #try:
            hosts = self.read_hosts()
            while True:
                for host in hosts:
                    print(host)
                    params = " -l 1000"
                    response = os.system("ping  " + host + params)
                    print("weiter")
                    self.update.emit(host, response)

        #except Exception as ex:
            #self.exception.emit(str(ex))

class Window(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)

        self.counter = 0
        self.items = self.read_hosts()
        self.layout = QGridLayout()
        for item in self.items:
            self.item = QLabel(item, self)
            self.item.setObjectName("label" + str(self.counter))
            print("label" + str(self.counter))
            self.item.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
            self.item.setAlignment(Qt.AlignCenter)
            self.item.setStyleSheet("QLabel {background-color: green;}")
            self.layout.addWidget(self.item,self.counter, 0)
            self.counter += 1
        self.setLayout(self.layout)
        self.startWorker()
        self.show()

    def startWorker(self):
        self.thread = QThread() 
        self.obj = Worker()  
        self.thread = QThread() 
        self.obj.moveToThread(self.thread)
        self.obj.update.connect(self.onUpdate)
        self.obj.exception.connect(self.onException)
        self.thread.started.connect(self.obj.check_ping)
        self.thread.start()

    def read_hosts(self):
        #try:
            file = open("hosts.ini", "r") 
            return file.readlines() 
        #except Exception as ex:
            #self.exception.emit(str(ex))

    def onException(self, msg):
        QMessageBox.warning(self, "Eine Exception im Worker wurde geworfen: ", msg)

    def onUpdate(self, lb, i):
        label = lb
        self.label0.setText("{}".format(i))


app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())

hosts.ini:

192.168.1.1
192.168.1.2
192.168.1.30

1 个答案:

答案 0 :(得分:1)

我在以下方面改进了您的代码:

  • 如果您的布局有列,则无需使用QGridLayout,只需QVBoxLayout

  • 对每个课程采用read_hosts()方法是浪费,所以我创建了一个独特的功能。

  • self.item是该类的一个属性,不断被覆盖,因此无需创建它们。

  • objectName应该是主机的名称,即IP,因为这是该主题所具有的信息。

  • 要通过objectName查找标签,您可以使用findChild()

import sys
import os

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

def read_hosts():
    file = open("hosts.ini", "r") 
    return file.readlines() 

class Worker(QObject):
    update = pyqtSignal(str, int)
    exception = pyqtSignal(str)

    def check_ping(self):
        hosts = read_hosts()
        while True:
            for host in hosts:
                params = "-l 1000"
                response = os.system("ping  {} {}".format(host, params))
                print("weiter")
                self.update.emit(host, response)

class Window(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)
        self.layout = QVBoxLayout(self)

        hosts = read_hosts()
        for host in hosts:
            label = QLabel(host)
            label.setObjectName(host)
            label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
            label.setAlignment(Qt.AlignCenter)
            label.setStyleSheet("QLabel {background-color: green;}")
            self.layout.addWidget(label)
        self.startWorker()

    def startWorker(self):
        self.thread = QThread() 
        self.obj = Worker()  
        self.thread = QThread() 
        self.obj.moveToThread(self.thread)
        self.obj.update.connect(self.onUpdate)
        self.obj.exception.connect(self.onException)
        self.thread.started.connect(self.obj.check_ping)
        self.thread.start()

    def onException(self, msg):
        QMessageBox.warning(self, "Eine Exception im Worker wurde geworfen: ", msg)

    def onUpdate(self, host, value):
        label = self.findChild(QLabel, host)
        label.setText("{}".format(value))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())