如何更新QWidget的内容?

时间:2018-11-12 20:58:12

标签: python python-3.x pyqt pyqt5 python-3.6

有一个QWidget,通过主窗口小部件中的“开始”按钮调用。

它显示一些文本,在QWidget工作期间应每分钟更新一次。

如何在此代码中进行无限更新?

class ExampleWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__()
        self.setWindowTitle('Example Widget ScrollArea')
        self.initUi()

    def initUi(self):
        area = QScrollArea(self)
        area.setWidgetResizable(True)
        self.scrollAreaWidgetContents = QLabel(some_text, self) 

        area.setWidget(self.scrollAreaWidgetContents)
        button = QPushButton("Close")
        button.clicked.connect(self.goMainWindow) 

        layoutV = QVBoxLayout() 
        layoutV.addWidget(area)
        layoutV.addWidget(button)
        self.setLayout(layoutV)

    def goMainWindow(self):
        self.hide()

    def sizeHint(self):                                  
        return QSize(400, 200)

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        start_main_button  = QPushButton('Start', self)
        start_main_button.move(40, 40)
        start_main_button.clicked.connect(self.start)
        self.setGeometry(300, 300, 290, 150)
        self.setWindowTitle('Test')

    def start(self):
        global some_text
        some_text = 'some text'
        self.result_widget = ExampleWidget()
        self.result_widget.show()
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

我的尝试之一:

def start(self):
        global some_text
        some_text = 'some text'
        self.result_widget = ExampleWidget()
        self.result_widget.show()
        i = 0
        while True:
            i+=1
            some_text+=str(i)
            self.result_widget = ExampleWidget()
            self.result_widget.show()

1 个答案:

答案 0 :(得分:0)

忘记全局变量是因为它们被认为是不好的做法,另一方面,在GUI中,您应该避免使用会浪费大量时间的循环,并且在这种情况下,True会阻塞GUI。在Qt中,如果要执行定期任务,则必须使用QTimer

from PyQt5 import QtCore, QtWidgets


class ExampleWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__()
        self.setWindowTitle('Example Widget ScrollArea')
        self.initUi()

    def initUi(self):
        area = QtWidgets.QScrollArea()
        area.setWidgetResizable(True)
        self.scrollAreaWidgetContents = QtWidgets.QLabel("some_text") 

        area.setWidget(self.scrollAreaWidgetContents)
        button = QtWidgets.QPushButton("Close")
        button.clicked.connect(self.hide) 

        layoutV = QtWidgets.QVBoxLayout(self) 
        layoutV.addWidget(area)
        layoutV.addWidget(button)

    def update_text(self, text):
        # update the text 
        self.scrollAreaWidgetContents.setText(text)

    def sizeHint(self):                                  
        return QtCore.QSize(400, 200)

class MainWindow(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        start_main_button  = QtWidgets.QPushButton('Start', self)
        start_main_button.move(40, 40)
        start_main_button.clicked.connect(self.start)

        self.setGeometry(300, 300, 290, 150)
        self.setWindowTitle('Test')
        self.result_widget = ExampleWidget()
        self.timer = QtCore.QTimer(self, interval=60*1000)
        self.timer.timeout.connect(self.on_timeout)
        self.counter = 0
        self.initial_text = "some_text"

    def on_timeout(self):
        # this method will be called every 60 * 1000 ms
        self.initial_text += str(self.counter)
        self.result_widget.update_text(self.initial_text)
        self.counter += 1

    @QtCore.pyqtSlot()
    def start(self):
        self.result_widget.show()
        # start timer
        self.timer.start()

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())