O
O
or latym2020-02-21 14:53:47
PyQt
or latym, 2020-02-21 14:53:47

How can you control the execution of animations?

I have this code

class test_mynu(QtWidgets.QWidget):
    def __init__(self, arr="", parent=None):
        super(test_mynu, self).__init__(parent)
        self._expand = False                                # - self.__expand ; + self._expand  !!!
        #------menu_pos_block------
        self.menu_pos_block = QtWidgets.QWidget()
        #------menu_pos_block_layout------
        self.menu_pos_block_layout = QtWidgets.QHBoxLayout(self.menu_pos_block)  
        self.menu_pos_block_layout.setObjectName(u"menu_pos_block_layout")
        #------menu_pos_label------
        self.menu_pos_label = QtWidgets.QLabel()
        self.menu_pos_label.setText(arr)
        self.menu_pos_block_layout.addWidget(self.menu_pos_label)
        #------ADD------
        lay = QtWidgets.QHBoxLayout(self)
        lay.addWidget(self.menu_pos_block)



class MainWindow(QtWidgets.QMainWindow):
    # number of radio
    def num(self):
        return 3
    def __init__(self, parent=None):
        super().__init__(parent)
        #------centralwidget------
        centralwidget = QtWidgets.QWidget() 
        centralwidget.setObjectName(u"centralwidget")
        self.setCentralWidget(centralwidget)
        #------layout------
        central_w_layout = QtWidgets.QGridLayout(centralwidget)
        central_w_layout.setObjectName(u"central_w_layout")
        #------Spacer------
        vs_up = QtWidgets.QSpacerItem(17, 105, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        central_w_layout.addItem(vs_up, 0, 1, 1, 1)
        hs_left = QtWidgets.QSpacerItem(327, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        central_w_layout.addItem(hs_left, 1, 0, 1, 1)
        hs_right = QtWidgets.QSpacerItem(326, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        central_w_layout.addItem(hs_right, 1, 2, 1, 1)
        vs_down = QtWidgets.QSpacerItem(20, 105, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        central_w_layout.addItem(vs_down, 2, 1, 1, 1)
        #------block------
        block = QtWidgets.QWidget()
        block.setObjectName(u"block")
        central_w_layout.addWidget(block, 1, 1, 1, 1)             ######
        #------layout------
        layout_block = QtWidgets.QVBoxLayout(block)  
        layout_block.setObjectName(u"layout_block")
        #------menu_block------
        menu_block = QtWidgets.QWidget()
        menu_block.setObjectName(u"menu_block")
        layout_block.addWidget(menu_block)             ######
        #------menu_layout------
        menu_layout = QtWidgets.QHBoxLayout(menu_block)  
        menu_layout.setObjectName(u"menu_layout")

        self.menu_but = []
        self.menu_pos = []
        for e in range(self.num()):
            #------menu_pos_block------
            menu_pos_block = test_mynu("menu_{}".format(e))
            self.menu_pos.append(menu_pos_block)
            #------ADD------
            menu_layout.addWidget(self.menu_pos[e])
        #------menu_block_but------
        self.menu_block_but = QtWidgets.QWidget()
        self.menu_block_but.setObjectName(u"menu_block_but")
        menu_layout.addWidget(self.menu_block_but)             ######
        #------menu_block_but_layout------
        menu_block_but_layout = QtWidgets.QVBoxLayout(self.menu_block_but)  
        menu_block_but_layout.setObjectName(u"menu_block_but_layout")
        #------menu_but_radio------
        for e in range(self.num()):
            menu_but_radio = QtWidgets.QRadioButton('{}'.format(e))
            menu_but_radio.setObjectName("radio_but_{}".format(e))
            self.menu_but.append(menu_but_radio)
            self.menu_but[e].toggled.connect(self.menu_animation)
            #------ADD------
            menu_block_but_layout.addWidget(self.menu_but[e])
        menu_block_content = QtWidgets.QWidget()
        menu_block_content.setObjectName(u"menu_block_content")
        menu_layout.addWidget(menu_block_content)             ######
        #------menu_content_layout------
        menu_content_layout = QtWidgets.QHBoxLayout(menu_block_content)  
        menu_content_layout.setObjectName(u"menu_content_layout")
        #------menu_label------
        menu_label = QtWidgets.QLabel()
        menu_label.setText('text, text text, text')
        menu_content_layout.addWidget(menu_label)
    def menu_animation(self):
        print("--x--")
        self.toggle_animations = QtCore.QSequentialAnimationGroup(self)
        for d in range(self.num()):
            print(str(d)+"_but_"+str(self.menu_but[d].isChecked()))
            print(str(d)+"_men_"+str(self.menu_pos[d]._expand))
            self.menu_pos[d].raise_()
            if self.menu_but[d].isChecked():
                self.anim = QPropertyAnimation(self.menu_pos[d], b"pos");
                self.anim.setDuration(1000);
                self.anim.setStartValue(QPoint(self.menu_block_but.x() - self.menu_pos[d].width(), self.menu_pos[d].y()));
                self.anim.setEndValue(QPoint(self.menu_block_but.x() +self.menu_block_but.width(), self.menu_pos[d].y()));
                #self.anim.start();
                self.menu_pos[d]._expand = True
            elif self.menu_pos[d]._expand and (not self.menu_but[d].isChecked()):
                self.anim = QPropertyAnimation(self.menu_pos[d], b"pos");
                self.anim.setDuration(1000);
                self.anim.setStartValue(QPoint(self.menu_block_but.x() +self.menu_block_but.width(), self.menu_pos[d].y()));
                self.anim.setEndValue(QPoint(self.menu_block_but.x() - self.menu_pos[d].width(), self.menu_pos[d].y()));
                #self.anim.start();
                self.menu_pos[d]._expand = False
                self.toggle_animations.addAnimation(self.anim)
            self.toggle_animations.addAnimation(self.anim)
        self.toggle_animations.start()
        print("--x--")


StyleSheet = """
#centralwidget QWidget QWidget,
#centralwidget QWidget QWidget QWidget QWidget,
#centralwidget QWidget QWidget QWidget QWidget QWidget QWidget,
#centralwidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget,
#centralwidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget{
background:#000;
color:#fff;
}
#centralwidget QWidget,
#centralwidget QWidget QWidget QWidget,
#centralwidget QWidget QWidget QWidget QWidget QWidget,
#centralwidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget,
#centralwidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget{
background:#fff;
color:#000;
}
#click_me{
background:#000;
color:#fff;
}
"""

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    app.setStyleSheet(StyleSheet)
    w = MainWindow()
    w.resize(640, 570) #Size window
    w.show()
    sys.exit(app.exec_())


And I need that if I switch from menu_0 to menu_1, then at first the code should hide menu_0 and after the end of the animations, the menu_1 appearance animation will turn on. But for some reason, the code sets only one animation per button. Can this be fixed somehow?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question