写入数据库时​​如何运行加载启动画面

时间:2019-06-22 22:46:53

标签: python python-3.x pyqt pyqt5

程序在数据库中写入时如何运行加载对话框

class windows_gui(QMainWindow):
    def __init__(self):
        super(windows_gui, self).__init__()
        self.setGeometry(200 , 200 ,500 ,500)
        button = QPushButton("Open window" , self)
        button.clicked.connect(self.change_dial)


    def change_dial(self):
        self._thread = QThread(self)  # Move to thread to execute
        self._thread.started.connect(self.loading_window_fun)

    def loading_window_fun(self):
        try :
            gif = os.path.join('system' , 'icons' , 'loading.gif')
            self.loding_window = QDialog()
            self.loding_window.setWindowFlags(Qt.WindowFlags(Qt.SplashScreen) )
            self.loding_window.setStyleSheet("border: 0px;")


            self.movie = QMovie(gif, QByteArray(), self.loding_window )
            self.movie_screen = QLabel()
            self.movie_screen.setStyleSheet("border: 0px;")
            self.movie_screen.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
            self.movie_screen.setAlignment(Qt.AlignCenter)
            self.movie_screen.setFixedWidth(500)
            self.movie_screen.setFixedHeight(500)

            main_layout = QVBoxLayout()
            self.loding_window.setLayout(main_layout)
            main_layout.addWidget(self.movie_screen)
            self.movie.setCacheMode(QMovie.CacheAll)
            self.movie_screen.setMovie(self.movie)
            self.movie.start()
            self.movie.loopCount()


            self.loding_window.exec_()
        except Exception as e :
            print(e)

    def wirtetodb(self):
        for i in range(100):
            print(i)

1 个答案:

答案 0 :(得分:0)

如果要在执行任务时使用启动屏幕,则该任务应至少消耗1秒钟,如果用户不太可能无法看到窗口打开和关闭的速度,则该任务将耗费至少1秒钟。考虑到上述情况,此任务必须在另一个线程中执行,因此我创建了一个QObject,该QObject在执行任务之前和之后均发出信号,这些信号用于显示和关闭窗口。

import os
from PyQt5 import QtCore, QtGui, QtWidgets


class DatabaseWorker(QtCore.QObject):
    started = QtCore.pyqtSignal()
    finished = QtCore.pyqtSignal()

    @QtCore.pyqtSlot()
    def writeToDatabase(self):
        self.started.emit()
        for i in range(100):
            QtCore.QThread.msleep(10)
            print(i)
        self.finished.emit()


class Windows_GUI(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setGeometry(200, 200, 500, 500)
        button = QtWidgets.QPushButton("Open window")
        self.setCentralWidget(button)
        thread = QtCore.QThread(self)
        thread.start()
        self.m_database_worker = DatabaseWorker()
        self.m_database_worker.moveToThread(thread)
        self.m_database_worker.started.connect(self.start_animation)

        button.clicked.connect(self.m_database_worker.writeToDatabase)

    @QtCore.pyqtSlot()
    def start_animation(self):

        gif_path = os.path.join("system", "icons", "loading.gif")
        self.loading_window = QtWidgets.QDialog()
        self.loading_window.setWindowFlags(QtCore.Qt.SplashScreen)
        self.loading_window.setAttribute(QtCore.Qt.WA_DeleteOnClose)

        movie = QtGui.QMovie(gif_path, cacheMode=QtGui.QMovie.CacheAll)

        movie_label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)
        movie_label.setStyleSheet("border: 0px;")
        movie_label.setSizePolicy(
            QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding
        )
        movie_label.setFixedSize(500, 500)
        movie_label.setMovie(movie)

        vbox = QtWidgets.QVBoxLayout(self.loading_window)
        vbox.addWidget(movie_label)
        self.m_database_worker.finished.connect(self.loading_window.close)
        self.loading_window.show()
        movie.start()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    w = Windows_GUI()
    w.show()
    sys.exit(app.exec_())

更新:

import os
from functools import partial
from PyQt5 import QtCore, QtGui, QtWidgets


class DatabaseWorker(QtCore.QObject):
    started = QtCore.pyqtSignal()
    finished = QtCore.pyqtSignal()

    @QtCore.pyqtSlot(object)
    def writeToDatabase(self, value):
        self.started.emit()
        for i in range(value):
            QtCore.QThread.msleep(10)
            print(i)
        self.finished.emit()


class Windows_GUI(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        # ...
        self.m_database_worker = DatabaseWorker()
        self.m_database_worker.moveToThread(thread)
        self.m_database_worker.started.connect(self.start_animation)

        button.clicked.connect(self.on_clicked)

    @QtCore.pyqtSlot()
    def on_clicked(self):
        value = 1000
        wrapper = partial(self.m_database_worker.writeToDatabase, value)
        QtCore.QTimer.singleShot(0, wrapper)

    # ...