PyQt5:如何在短时间内显示另一个窗口/小部件

时间:2019-10-18 14:07:14

标签: python pyqt5

您好,在我的应用程序中,我有主窗口,当数据来自另一个线程时,我需要在另一个屏幕上显示2秒钟,然后返回上一个屏幕。屏幕具有许多组件,因此我制作了一个简单的版本来演示我的目的。

数据成功地来自另一个线程,我可以更改标签的文本。但是,我不能使旧的消失而新的消失。

from PyQt5.QtCore import QThread
from PyQt5.QtWidgets import QApplication,QMainWindow, QLabel, QWidget, QGridLayout, QVBoxLayout, QGroupBox
from PyQt5.QtGui import QTextDocument
from PyQt5 import QtCore, Qt
from PyQt5.QtGui import QIcon, QPixmap, QFont
from time import strftime 
import datetime 
from babel.dates import format_date, format_datetime, format_time
import sys
import worker
import time


class Form(QWidget):

    def __init__(self):
        super().__init__()
        self.label_main = QLabel("Welcome")
        self.label_uid = QLabel("Exit") 
        self.left = 0
        self.top = 0
        self._width = 480
        self._height = 800

        self.layout_main = QVBoxLayout()
        self.layout_access = QVBoxLayout()

        self.obj = worker.Worker()  # no parent!
        self.thread = QThread()  # no parent!
        self.obj.return_uid.connect(self.onCardRead)     
        self.obj.moveToThread(self.thread)
        self.obj.finished.connect(self.thread.quit)
        self.thread.started.connect(self.obj.get_uid)
        self.thread.start()

        self.initUI()

    def initUI(self):
        self.setLayout(self.layout_main)
        self.layout_main.addWidget(self.label_main)
        self.setWindowTitle('Main Thread')
        self.show()

    def secondUI(self):
        self.setLayout(self.layout_access)
        self.layout_access.addWidget(self.label_uid)
        self.setWindowTitle('Access Thread')
        self.show()

2 个答案:

答案 0 :(得分:0)

在show()之后添加self.windowname.close()或仅self.close()

答案 1 :(得分:0)

尝试一下:

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


class Worker(QtCore.QObject):
    return_uid = QtCore.pyqtSignal(int)
    finished = QtCore.pyqtSignal()

    def get_uid(self): 
        print("start")
        count = 0
        QtCore.QThread.msleep(1000)
        while count < 10:
            QtCore.QThread.msleep(200)      
            self.return_uid.emit(count)
            count += 1
        self.finished.emit()


class Form(QWidget):
    def __init__(self):
        super().__init__()
        self.label_main = QLabel("Welcome")
        self.label_uid = QLabel("Exit") 

        self.layout_main = QVBoxLayout()
        self.layout_access = QVBoxLayout()

#        self.obj = worker.Worker()  # no parent!
        self.obj = Worker()                           

        self.thread = QThread()     
        self.obj.return_uid.connect(self.onCardRead)     
        self.obj.moveToThread(self.thread)

#        self.obj.finished.connect(self.thread.quit)
        self.obj.finished.connect(self.close)

        self.thread.started.connect(self.obj.get_uid)
        self.thread.start()

        self.initUI()

    def initUI(self):
        self.setLayout(self.layout_main)
        self.layout_main.addWidget(self.label_main)
        self.setWindowTitle('Main Thread')
        self.show()

    def secondUI(self):
        self.setLayout(self.layout_access)
        self.layout_access.addWidget(self.label_uid)
        self.setWindowTitle('Access Thread')
        self.show()

    def onCardRead(self, id):
        self.label_main.setNum(id)            


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Form()
    sys.exit(app.exec_())        

enter image description here


更新

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


class Worker(QtCore.QObject):
    return_uid = QtCore.pyqtSignal(int)
    finished = QtCore.pyqtSignal()

    def get_uid(self): 
        print("start")
        count = 0
        QtCore.QThread.msleep(1000)
        while count < 10:
            QtCore.QThread.msleep(500)      
            self.return_uid.emit(count)
            count += 1
        self.finished.emit()


class Form(QWidget):
    def __init__(self):
        super().__init__()
        self.label_main = QLabel("Welcome")
        self.label_uid = QLabel("Exit") 

        self.layout_main = QVBoxLayout()
#        self.layout_access = QVBoxLayout()

        self.obj = Worker()                           
        self.thread = QThread()     
        self.obj.return_uid.connect(self.onCardRead)     
        self.obj.moveToThread(self.thread)

#        self.obj.finished.connect(self.close)
        self.obj.finished.connect(self.secondUI)                # <---

        self.thread.started.connect(self.obj.get_uid)
        self.thread.start()

        self.initUI()

    def initUI(self):
        self.setLayout(self.layout_main)
        self.layout_main.addWidget(self.label_main)
        self.setWindowTitle('Main Thread')
        self.resize(300, 100)
        self.show()

    def secondUI(self):                                         # <---
        self.hide()
        self.windowSecond = QWidget()
        self.layout_access = QVBoxLayout(self.windowSecond)
        self.layout_access.addWidget(self.label_uid)
        self.windowSecond.setWindowTitle('Main Screen')
        self.windowSecond.resize(300, 200)
        self.windowSecond.show()

    def onCardRead(self, id):
        self.label_main.setNum(id)            


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Form()
    sys.exit(app.exec_()) 

enter image description here