2012-08-27 38 views
2

我已經實現了一個Python簡單的應用程序,沒有任何動畫。 現在我想添加一個簡單的動畫,由一個信號觸發(例如一個按鈕點擊),觸發時會放大窗口的寬度並顯示一個新文本區域,其中包含一些文本。PyQt4 - 添加文本編輯區域動畫示例

老實說,我對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() 

,這擴大了我的窗前,我想。

不幸的是我不知道如何插入一個新的文本區域,避免已經存在,以填補新的空間小部件(實際上,當窗口放大,窗口小部件使用 所有的空間,從而擴大自己)

有人可以幫助我知道如何添加文本區域外觀動畫?

任何幫助表示讚賞......真的達到這個

回答

0

一種方法是動畫窗口和文本編輯既對maximumWidth財產。

主要困難在於,它能夠以標準佈局很好地播放,同時也允許調整窗口大小。在動畫過程中避免閃爍也非常棘手。

下面的演示是幾乎沒有(在動畫微翹的開始和結束生澀):

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_())