简体   繁体   English

如何在 PyQt5 中创建循环和自动关闭消息框?

[英]How to make a loop and AutoClosed messagebox in PyQt5?

How to make a loop and AutoClosed messagebox in PyQt5?如何在 PyQt5 中创建循环和自动关闭消息框?

Here is a kind of messagebox that can auto close after 3sec.这是一种可以在 3 秒后自动关闭的消息框。

I want to show the message by using it in a loop.我想通过在循环中使用它来显示消息。

How can i do it?我该怎么做?

PS:the code is not writen by me PS:代码不是我写的

import sys

from PyQt5 import QtCore, QtGui, QtWidgets, Qt
import time

class Ui_Message(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(538, 91)
        self.frame = QtWidgets.QFrame(Form)
        self.frame.setGeometry(QtCore.QRect(0, 0, 541, 111))
        # self.frame.setStyleSheet("background-image: url(:/img/messageback.png);")
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.label = QtWidgets.QLabel(self.frame)
        self.label.setGeometry(QtCore.QRect(0, 0, 531, 91))
        font = QtGui.QFont()
        font.setPointSize(31)
        font.setBold(False)
        font.setWeight(50)
        self.label.setFont(font)
        self.label.setStyleSheet("background-color: transparent;\n"
                                 "fontsize: 30px;")
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "显示信息"))

# import img_rc


# Define a removable borderless 3S prompt message interface
class MessageWindow(Qt.QMainWindow):
    def __init__(self, parent=None):
        Qt.QWidget.__init__(self, parent)
        self.ui = Ui_Message()
        self.ui.setupUi(self)
        self.setWindowFlags(Qt.Qt.FramelessWindowHint)
        QtCore.QTimer().singleShot(3000, self.close)
        self.show()

    def mousePressEvent(self, event):
        # Define mouse click events
        if event.button() == QtCore.Qt.LeftButton:
            self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
            event.accept()

    def mouseMoveEvent(self, event):
        # Define mouse movement events
        if event.buttons() == QtCore.Qt.LeftButton:
            self.move(event.globalPos() - self.dragPosition)
            event.accept()

    def setMessage(self, message):
        self.ui.label.setText(message)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    login = MessageWindow()
    #----how can i maike a loop messagebox?------
    for i in range(0,5):
        login.setMessage("Number"+str(i))
        time.sleep(3)
    sys.exit(app.exec())

I know maybe i can't use time.sleep in PyQt5,however i had search for anything but not get the answer.我知道也许我不能在 PyQt5 中使用 time.sleep,但是我搜索了任何东西但没有得到答案。

In Qt if you want to do a periodic task then you must use a QTimer, and forget about synchronous logic since you must work through events.在 Qt 中,如果您想执行周期性任务,那么您必须使用 QTimer,并且忘记同步逻辑,因为您必须处理事件。

In this case a possible solution is to use a queue that stores the information and in each shot of the timer an element is obtained.在这种情况下,一种可能的解决方案是使用存储信息的队列,并且在计时器的每个镜头中都获得一个元素。

from collections import deque
if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    login = MessageWindow()
    values = range(0, 5)
    q = deque(values)

    def on_timeout():
        print(q)
        if q:
            i = q.popleft()
            Qt.QTimer.singleShot(3000, on_timeout)
            login.setMessage("Number" + str(i))

    on_timeout()

    sys.exit(app.exec())

Notes:笔记:

  • Your class must not inherit from QMainWindow but from QWidget.您的 class 不能从 QMainWindow 继承,而是从 QWidget 继承。
  • You must eliminate the timer that closes the window, otherwise you will not see the text change since the time is very short.您必须消除关闭 window 的计时器,否则您将看不到文字变化,因为时间很短。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM