Skip to main content
 首页 » 编程设计

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

2025年05月04日8itcoder

我实现了一个简单的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_())