无名.py:
from PyQt5 import QtCore, QtGui, QtWidgets
from b import FlyButton
class Ui_MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setupUi()
def setupUi(self):
self.setObjectName("self")
self.resize(612, 427)
self.pusButton = FlyButton()
self.pusButton.setGeometry(QtCore.QRect(270, 192, 51, 41))
self.pusButton.setText("")
self.pusButton.setObjectName("pusButton")
self.setCentralWidget(self.pusButton)
self.statusbar = QtWidgets.QStatusBar(self)
self.statusbar.setObjectName("statusbar")
self.setStatusBar(self.statusbar)
这里,有一些代码创建了带有某种动画的按钮,并且该按钮应该位于正确的位置,而不是位于整个窗口上。 (这是使用PyQt5 UI 代码生成器 5.15.9
生成的代码。我尝试保留此生成的代码并将其分配给另一个文件,但它不起作用。
我不明白为什么?我只是想知道如何使它不出现在整个窗口上?)FlyButton()self.pusButton
b.py:
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QPropertyAnimation, QEasingCurve
from PyQt5 import QtCore
class FlyButton(QPushButton):
def __init__(self, *args, **kwargs):
super().__init__()
self.animation = QPropertyAnimation(self, b'size')
self.animation.setDuration(2000)
self.animation.setEasingCurve(QEasingCurve.OutInElastic)
def enterEvent(self, event):
self.animation.setStartValue(self.size())
self.animation.setEndValue(QtCore.QSize(300, 250,))
self.animation.start()
QPushButton.enterEvent(self, event)
print('работает')
这是用户界面本身:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>612</width>
<height>427</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>270</x>
<y>192</y>
<width>51</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>