python - PyQt4——添加文本编辑区动画示例

标签 python animation resize pyqt4

我实现了一个简单的python应用程序,上面没有任何动画。 现在我想添加一个简单的动画,由信号触发(例如单击按钮),触发时会扩大窗口的宽度并显示一个包含一些文本的新文本区域。

老实说,我是python/pyqt4的新手,对动画框架了解不多。

我试图将它添加到我的类代码中,例如在一个名为点击关于菜单的方法中:) :

  self.anim = QPropertyAnimation(self, "size")
  self.anim.setDuration(2500)
  self.anim.setStartValue(QSize(self.width(), self.height()))
  self.anim.setEndValue(QSize(self.width()+100, self.height()))
  self.anim.start()

这会根据需要放大我的窗口。

不幸的是我不知道如何插入一个新的文本区域,避免已经存在的小部件来填充新的空间(实际上,当窗口放大时,小部件使用 所有的空间,从而扩大自己)

谁能帮我知道如何添加文本区域外观动画?

感谢任何帮助...真的...

最佳答案

实现此目的的一种方法是在窗口和文本编辑器上为 maximumWidth 属性设置动画。

主要的困难在于如何以一种与标准布局很好地兼容的方式进行操作,同时还允许调整窗口大小。在动画过程中避免闪烁也很棘手。

下面的demo就差不多了(动画开头结尾有点生涩):

from PyQt4 import QtGui, QtCore

class Window(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        self._offset = 200
        self._closed = False
        self._maxwidth = self.maximumWidth()
        self.widget = QtGui.QWidget(self)
        self.listbox = QtGui.QListWidget(self.widget)
        self.button = QtGui.QPushButton('Slide', self.widget)
        self.button.clicked.connect(self.handleButton)
        self.editor = QtGui.QTextEdit(self)
        self.editor.setMaximumWidth(self._offset)
        vbox = QtGui.QVBoxLayout(self.widget)
        vbox.setContentsMargins(0, 0, 0, 0)
        vbox.addWidget(self.listbox)
        vbox.addWidget(self.button)
        layout = QtGui.QHBoxLayout(self)
        layout.addWidget(self.widget)
        layout.addWidget(self.editor)
        layout.setSizeConstraint(QtGui.QLayout.SetMinAndMaxSize)
        self.animator = QtCore.QParallelAnimationGroup(self)
        for item in (self, self.editor):
            animation = QtCore.QPropertyAnimation(item, 'maximumWidth')
            animation.setDuration(800)
            animation.setEasingCurve(QtCore.QEasingCurve.OutCubic)
            self.animator.addAnimation(animation)
        self.animator.finished.connect(self.handleFinished)

    def handleButton(self):
        for index in range(self.animator.animationCount()):
            animation = self.animator.animationAt(index)
            width = animation.targetObject().width()
            animation.setStartValue(width)
            if self._closed:
                self.editor.show()
                animation.setEndValue(width + self._offset)
            else:
                animation.setEndValue(width - self._offset)
        self._closed = not self._closed
        self.widget.setMinimumSize(self.widget.size())
        self.layout().setSizeConstraint(QtGui.QLayout.SetFixedSize)
        self.animator.start()

    def handleFinished(self):
        if self._closed:
            self.editor.hide()
        self.layout().setSizeConstraint(QtGui.QLayout.SetMinAndMaxSize)
        self.widget.setMinimumSize(0, 0)
        self.setMaximumWidth(self._maxwidth)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.move(500, 300)
    window.show()
    sys.exit(app.exec_())

关于python - PyQt4——添加文本编辑区动画示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12137472/

相关文章:

python - 查询集对象没有属性 'Name'

javascript - 如何每次 .animate 重复结束时执行某些操作

java - 循环数组队列实现 : Which is the best way to resize a circular array?

HTML CSS : div element height 100% when resized to + resolution

python - Numpy 调整大小/重新缩放图像

Python 处理 HTTP 请求

Python:将数组的几列合并为一列的更简单方法

ios - 第一次键盘出现时的动画有时不流畅

python - 在 PC/Linux 上使用 Python 读取 Quicktime 电影的开始时间码

vba - 用VBA读取PowerPoint动画效果