Почему моя программа вылетает после анимации на PyQt5
Я сделал не большую анимацию на PyQt5. И мне нужно было чтобы при окончании анимации программа открыла другое окно. Но почему-то при завершении анимации программа завершается.
main.py
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from creator import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowFlags(Qt.WindowType.Window | Qt.WindowType.FramelessWindowHint)
self.setWindowTitle("PANDA")
self.resize(300, 300)
self.setWindowOpacity(0.1)
pixmap1 = QPixmap('back3.png')
pal = self.palette()
pal.setBrush(QPalette.ColorGroup.Normal, QPalette.ColorRole.Window,
QBrush(pixmap1))
pal.setBrush(QPalette.ColorGroup.Inactive,
QPalette.ColorRole.Window, QBrush(pixmap1))
self.setPalette(pal)
self.setMask(pixmap1.mask())
box = QHBoxLayout()
lbl = QLabel("Panda ", self)
lbl.setFixedSize(90, 30)
font = QFont("Tahoma", 25)
lbl.setFont(font)
#btn.clicked.connect(QApplication.instance().quit)
box.addWidget(lbl)
self.setLayout(box)
self.animation = QPropertyAnimation(self, b'windowOpacity')
self.animation.setDuration(1000)
self.animation.stop()
self.animation.finished.connect(self.anim)
self.animation.setStartValue(0)
self.animation.setEndValue(1)
self.animation.start()
def anim(self):
self.animation.stop()
self.animation.finished.connect(self.close)
self.animation.setStartValue(1)
self.animation.setEndValue(0)
self.animation.start()
self.running()
def running(self):
cre_win = Creator_Widget()
cre_win.show()
class Creator_Widget(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Panda studio")
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Продлите жизнь объекту cre_win, сделайте его атрибутом класса:
...
# ----> vvvv <-------------------------------------------------- # !!!
self.cre_win = Creator_Widget()
...
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
# ??? from creator import *
class Creator_Widget(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Panda studio")
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowFlags(
Qt.WindowType.Window | Qt.WindowType.FramelessWindowHint)
self.setWindowTitle("PANDA")
self.resize(1020, 510)
self.setWindowOpacity(0.1)
pixmap1 = QPixmap('panda.jpg')
pal = self.palette()
pal.setBrush(QPalette.ColorGroup.Normal, QPalette.ColorRole.Window,
QBrush(pixmap1))
pal.setBrush(QPalette.ColorGroup.Inactive,
QPalette.ColorRole.Window, QBrush(pixmap1))
self.setPalette(pal)
self.setMask(pixmap1.mask())
lbl = QLabel("Panda ", alignment=Qt.AlignCenter)
# lbl.setFixedSize(90, 60)
font = QFont("Tahoma", 45)
lbl.setFont(font)
lbl.setStyleSheet('color: red;')
box = QHBoxLayout(self)
box.addWidget(lbl)
self.animation = QPropertyAnimation(self, b'windowOpacity')
self.animation.setDuration(5000)
self.animation.stop()
self.animation.finished.connect(self.anim)
self.animation.setStartValue(0)
self.animation.setEndValue(1)
self.animation.start()
def anim(self):
''' ??? ?
self.animation.stop()
self.animation.finished.connect(self.close)
self.animation.setStartValue(1)
self.animation.setEndValue(0)
self.animation.start()
'''
self.running()
def running(self):
# ----> vvvv <-------------------------------------------------- # !!!
self.cre_win = Creator_Widget()
self.cre_win.show()
self.close() # +
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())

