PyQt: 如何动画显示对话框大小

2024-09-30 04:35:16 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的代码创建一个QWidget窗口,其中QPushButton连接到resizeDialog()。按一个按钮将对话框的大小从.resize(200100)切换到.resize(600300),然后返回。在

问:默认情况下,从一个窗口大小到另一个窗口的转换会立即发生。如何用动画覆盖此行为?在

enter image description here

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

Tags: importselfappinitdefsysbuttonparent
1条回答
网友
1楼 · 发布于 2024-09-30 04:35:16
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()

this问题链接的source将导致以下代码。在这种情况下,从c++代码到python的转换相当简单。在

相关问题 更多 >

    热门问题