Увеличение и уменьшение кнопки, анимация
Для кнопки self.pushButtonProfile я создал анимацию на увеличение при нажатии, а при втором нажатии она должна вернуться.
Обратный возврат height кнопки не работает.
Код написан на PyQt6:
class AboutTheProgram(QDialog, Ui_AboutTheProgram):
def __init__(self):
super().__init__()
self.nameUsers = None
self.oldPos = None
self.ui = None
self.logo_text = None
self.abouttheprogram = Ui_AboutTheProgram
self.setupUi(self)
self.importmainclass = MainWindows
self.importregclass = Refistration
self.nameUsers = os.getlogin()
if os.path.isfile(f'/Users/{self.nameUsers}\Downloads/soft.txt'):
os.remove(f'/Users/{self.nameUsers}\Downloads/soft.txt')
print("success")
else:
print("File doesn't exists!")
#Импорт основных методов передвижение окна windows и убарать windows элементы из виджета.
self.importmainclass.RemoveWindowsMenu(self)
self.pushClose.clicked.connect(self.importmainclass.CloseWindow)#кнопка завершение программы
self.pushCollapse.clicked.connect(self.showMinimized)#Сворачивание окна
self.pushExit.clicked.connect(self.PushBack)#кнопка для выхода с аккаунта
self.pushDownload.clicked.connect(self.download)
#Работа с кнопкой профиль
self.layout = QVBoxLayout()
self.layout.addWidget(self.pushButtonProfile)
self.setLayout(self.layout)
self.pushButtonProfile.clicked.connect(self.on_button_clicked)
self.animation = None
self.is_expanded = False
def on_button_clicked(self):
if not self.animation or self.animation.state() == QAbstractAnimation.State.Stopped:
if not self.is_expanded:
self.animation = QPropertyAnimation(self.pushButtonProfile, b"minimumSize")
self.animation.setDuration(300)
self.animation.setStartValue(QSize(self.pushButtonProfile.width(), 31))
self.animation.setEndValue(QSize(self.pushButtonProfile.width(), 111))
self.animation.start()
self.is_expanded = True
else:
self.animation = QPropertyAnimation(self.pushButtonProfile, b"minimumSize")
self.animation.setDuration(600)
self.animation.setStartValue(QSize(self.pushButtonProfile.width(), 111))
self.animation.setEndValue(QSize(self.pushButtonProfile.width(), 31))
self.animation.start()
self.is_expanded = False
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Вы почему-то не предоставляете минимально-воспроизводимый пример, который демонстрирует проблему.
Я думаю, что вам надо установите анимацию на геометрию, а не на размер.
import sys
from PyQt5.Qt import *
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.btn = QPushButton('click', self)
self.btn.setCheckable(True) # !!!
self.btn.resize(100, 31)
self.btn.move(125, 125)
self.btn.clicked.connect(self.btn_clicked)
def increase(self):
rect_start = QRect(self.btn.geometry())
rect_end = QRect(0, 0, 100, 31)
rect_end.moveCenter(rect_start.center())
# ---------------------------------------------> vvvvvvvvvvv <------------ !!!
animation = QPropertyAnimation(self.btn, b"geometry", self)
animation.setStartValue(rect_start)
animation.setEndValue(rect_end)
animation.setDuration(1000)
animation.start(QAbstractAnimation.DeleteWhenStopped)
def reduce(self):
rect_start = QRect(self.btn.geometry())
rect_end = QRect(0, 0, 100, 111)
rect_end.moveCenter(rect_start.center())
# ---------------------------------------------> vvvvvvvvvvv <------------ !!!
animation = QPropertyAnimation(self.btn, b"geometry", self)
animation.setStartValue(rect_start)
animation.setEndValue(rect_end)
animation.setDuration(1000)
animation.start(QAbstractAnimation.DeleteWhenStopped)
def btn_clicked(self, state): # !!! state
if state:
self.reduce()
else:
self.increase()
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setStyle("Fusion")
w = MainWindow()
w.resize(400, 400)
w.show()
sys.exit(app.exec())


