gpt4 book ai didi

python - 在pyqt中运行另一个进程时旋转动画

转载 作者:行者123 更新时间:2023-12-03 13:16:51 25 4
gpt4 key购买 nike

我有一个简单的pyqt5应用程序,该应用程序读取大* .json文件并制作键的树列表 View 。
想知道如何同时制作动画和相对较长的json阅读任务。
因此,我需要一个轮子在读取文件时旋转。

看起来它必须是线程,但我还不熟悉。

这是一个代码:

import sys 
import os
import json
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QLabel, QListView, QTreeWidget, QTreeWidgetItem
from PyQt5.QtCore import *

current_folder = os.path.dirname(os.path.realpath(__file__))
load_icon = os.path.join(current_folder, 'icon_load.png')

class loaderDialog(QWidget):
def __init__(self, parent=None):
super(ren_loaderDialog, self).__init__(parent)
self.initUI()


def get_values_dict(self):
"""Getting data of unique values using in tables
and making a dict of mappings for specific table
Operation can take a long time
"""

script_folder = current_folder
file_local = os.path.join(script_folder, 'attributes.json')

with open(file_local, 'r', encoding='utf-8') as fp:
data = json.load(fp)

return data


def initUI(self):
"""Set GUI and get all data for launching
"""

self.setWindowTitle('Layer loader')
self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
self.grid = QGridLayout()
self.grid.setSpacing(10)
self.setGeometry(500, 500, 400, 520)

self.listView = QTreeWidget()
self.listView.setHeaderLabel('Layers')

self.setLayout(self.grid)
self.grid.addWidget(self.listView, 0, 1, 1, 2)

self.case_strings = self.get_values_dict()
self.load_data_to_tree(self.case_strings)

self.show()


def loading_fig(self):
"""Animation of rotating wheel
"""

self.spin_wheel_init = QLabel()
self.spin_wheel_init.setAlignment(QtCore.Qt.AlignCenter)
self.spin_wheel_init.setPixmap(QPixmap(load_icon))
self.grid.addWidget(self.spin_wheel_init, 0, 1, 1, 2)
angle = 0

while True:
tr = QTransform().rotate(angle)
angle = angle + 1 if angle<360 else 0
self.spin_wheel_init.setPixmap(QPixmap(load_icon).transformed(tr))
time.sleep(0.001)
QtCore.QCoreApplication.processEvents()


def load_data_to_tree(self, data):
"""Giving keys to treeview
"""
for name in data:
child = QTreeWidgetItem(self.listView)
child.setFlags(child.flags() | Qt.ItemIsSelectable | Qt.ItemIsUserCheckable)
child.setText(0, name)
child.setCheckState(0, Qt.Unchecked)


if __name__ == "__main__":
app = QApplication(sys.argv)
w = loaderDialog()
w.show()
sys.exit(app.exec_())

在这里,我需要在调用 loading_fig函数读取文件时使 get_values_dict函数正常工作。

最佳答案

您应该避免在UI线程中使用循环和sleep

运行长时间函数的常见方法是创建另一个线程。您可以使用QThread Qt类(如果愿意,也可以使用Python)来实现。

要使用QThread,请创建一个继承自Worker的新类QObject。它将包含您的过程。然后,创建一个实例并将其移至另一个线程:

class Worker(QObject):
done = pyqtSignal(list)

def __init__(self, parent=None):
super().__init__(parent)

def doWork(self):
print("Start")
time.sleep(10)
self.done.emit(['one', 'two', 'three'])
print("done")
class loaderDialog(QWidget):
def __init__(self, parent=None):
super(loaderDialog, self).__init__(parent)
self.initUI()
self.thread = QThread(self)
self.worker = Worker()
self.worker.moveToThread(self.thread) # worker will be runned in another thread
self.worker.done.connect(self.load_data_to_tree) # Call load_data_to_tree when worker.done is emitted
self.thread.started.connect(self.worker.doWork) # Call worker.doWork when the thread starts
self.thread.start() # Start the thread (and run doWork)

对于微调器,您应该使用诸如 QPropertyAnimation之类的动画而不是循环。例如:
class Spinner(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setAlignment(QtCore.Qt.AlignCenter)
self.pixmap = QPixmap(load_icon)

self.setFixedSize(30, 30)
self._angle = 0

self.animation = QPropertyAnimation(self, b"angle", self)
self.animation.setStartValue(0)
self.animation.setEndValue(360)
self.animation.setLoopCount(-1)
self.animation.setDuration(2000)
self.animation.start()


@pyqtProperty(int)
def angle(self):
return self._angle

@angle.setter
def angle(self, value):
self._angle = value
self.update()


def paintEvent(self, ev=None):
painter = QPainter(self)
painter.translate(15, 15)
painter.rotate(self._angle)
painter.translate(-15, -15)
painter.drawPixmap(5, 5, self.pixmap)

关于python - 在pyqt中运行另一个进程时旋转动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58799257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com