下面的代码创建了一个QWidget窗口,其中QPushButton连接到resizeDialog()。按下按钮可将对话框的大小从.resize(200,100)切换到.resize(600,300)并返回。
问题:默认情况下,会立即从一个窗口的大小过渡到另一个窗口的大小。如何用动画覆盖这种行为?
from PyQt4 import QtCore, QtGui
import sys
class myWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(myWindow, self).__init__(parent)
myLayout = QtGui.QVBoxLayout(self)
Button = QtGui.QPushButton('Resize')
myLayout.addWidget(Button)
Button.setMinimumWidth(200)
Button.clicked.connect(self.resizeDialog)
def resizeDialog(self):
dialog.size().width()==200:
dialog.resize(600,300)
else:
dialog.resize(200,100)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
app.setApplicationName('myApp')
dialog = myWindow()
dialog.resize(200,100)
dialog.show()
sys.exit(app.exec_())
答案 0 :(得分:3)
def resizeDialog(self):
self.animation = QtCore.QPropertyAnimation(self, "size")
# self.animation.setDuration(1000) #Default 250ms
if self.size().width()==200:
self.animation.setEndValue(QtCore.QSize(600,300))
else:
self.animation.setEndValue(QtCore.QSize(200,100))
self.animation.start()