Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何处理大量小部件的QPropertyImation_Python_Python 3.x_Pyqt_Pyqt5_Qpropertyanimation - Fatal编程技术网

Python 如何处理大量小部件的QPropertyImation

Python 如何处理大量小部件的QPropertyImation,python,python-3.x,pyqt,pyqt5,qpropertyanimation,Python,Python 3.x,Pyqt,Pyqt5,Qpropertyanimation,我希望有许多彩色点在背景上不断移动。(下面的代码描述):PolkaDot小部件由随机颜色、大小、位置和持续时间构成。QPropertyAnimation将小部件在屏幕上从左向右移动,在动画结束时以新高度重新启动。100个PolkaDot小部件被构建在后台小部件中,这足以让它看起来像成吨的新点不断从屏幕左侧涌入 然而,100属性动画似乎消耗了大量CPU资源,导致其速度减慢,看起来不平滑。有没有其他方法可以达到类似的效果?尝试运行下面的代码 import sys, random from PyQt5

我希望有许多彩色点在背景上不断移动。(下面的代码描述):PolkaDot小部件由随机颜色、大小、位置和持续时间构成。QPropertyAnimation将小部件在屏幕上从左向右移动,在动画结束时以新高度重新启动。100个PolkaDot小部件被构建在后台小部件中,这足以让它看起来像成吨的新点不断从屏幕左侧涌入

然而,100属性动画似乎消耗了大量CPU资源,导致其速度减慢,看起来不平滑。有没有其他方法可以达到类似的效果?尝试运行下面的代码

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

colors = [QColor('#f00'), QColor('#00f'), QColor('#0f0'), QColor('#ff0'),
          QColor('#fff'), QColor('#ff6000'), QColor('#6b00ff'), QColor('#f0f')]

class PolkaDot(QWidget):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setFixedSize(50, 50)
        self.color = random.choice(colors)
        self.r = random.randrange(5, 22)
        self.move(random.randrange(w), random.randrange(h))
        self.anim = QPropertyAnimation(self, b'pos')
        self.anim.finished.connect(self.run)
        self.anim.setDuration(random.randrange(3000, 9000))
        self.anim.setStartValue(QPoint(self.x() - (w + 60), self.y()))
        self.anim.setEndValue(QPoint(w + 60, self.y()))
        self.anim.start()

    def paintEvent(self, event):
        qp = QPainter(self)
        qp.setRenderHint(QPainter.Antialiasing)
        qp.setBrush(self.color)
        qp.setPen(QPen(self.color.darker(130), self.r / 5))
        qp.drawEllipse(QPoint(25, 25), self.r, self.r)

    def run(self):
        y = random.randrange(h)
        self.anim.setDuration(random.randrange(3000, 9000))
        self.anim.setStartValue(QPoint(-60, y))
        self.anim.setEndValue(QPoint(w + 60, y + random.randrange(-50, 50)))
        self.anim.start()


class Background(QWidget):

    def __init__(self):
        super().__init__()
        self.setGeometry(window)
        polka_dots = [PolkaDot(self) for i in range(100)]
        self.setStyleSheet('background-color: #000')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = QDesktopWidget().availableGeometry()
    w, h = window.width(), window.height()
    gui = Background()
    gui.show()
    sys.exit(app.exec_())

我找到了一个解决办法。出于某种原因,在后台小部件的绘制事件中创建点并在QTimer.timeout()上调用repaint()来更新XY坐标比使用QPropertyImation要高效得多

tick = 24

class Dot(object):

    def __init__(self):
        self.x = random.randrange(-w - 60, 0)
        self.randomize()

    def randomize(self):
        self.color = random.choice(colors)
        self.r = random.randrange(5, 22)
        self.y = random.randrange(h)
        self.x_speed = random.randrange(3000, 9000)
        self.y_speed = random.randrange(-50, 50)

    def move(self):
        self.x += w * tick / self.x_speed
        self.y += self.y_speed * tick / self.x_speed
        if self.x > w:
            self.x = -60
            self.randomize()


class Background(QWidget):

    def __init__(self):
        super().__init__()
        self.setGeometry(window)
        self.setStyleSheet('background-color: #000')
        self.dots = [Dot() for i in range(150)]
        self.timer = QTimer()
        self.timer.setInterval(tick)
        self.timer.timeout.connect(self.animate)
        self.timer.start()

    def paintEvent(self, event):
        qp = QPainter(self)
        qp.setRenderHint(QPainter.Antialiasing)
        for dot in self.dots:
            qp.setBrush(dot.color)
            qp.setPen(QPen(dot.color.darker(130), dot.r / 5))
            qp.drawEllipse(QPoint(dot.x, dot.y), dot.r, dot.r)

    def animate(self):
        for d in self.dots:
            d.move()
        self.repaint()

你不能仅仅通过命名来ping版主;我在这里看到的评论只是因为它被标记了。请在帖子上使用自定义版主标志,请求版主对我们的行为进行反馈。抱歉,由于审核原因,该特定帐户已被删除。