как я могу переключить раскладку с сплиттер в pyqt5?

голоса
0

Я хочу , чтобы переключатель правого макета рамы с разветвителем, как я могу сделать? Я пытаюсь с помощью QStackedWidget, но он не в состоянии . Ниже мой код:

нарисовать макет

def draw_layout(self):
        try:
            main_layout = QVBoxLayout(self)

            #create frame of main tab 
            tab_layout = QHBoxLayout(self)

            left_frame = QFrame(self)
            right_frame = QFrame(self) 
            right_frame2 = QFrame(self) 

            Stack = QStackedWidget(self)
            Stack.addWidget(right_frame)
            Stack.addWidget(right_frame2)

            left_frame.setFrameShape(QFrame.StyledPanel)
            right_frame.setFrameShape(QFrame.StyledPanel)
            right_frame2.setFrameShape(QFrame.StyledPanel)

            self.right_frame = right_frame2

            # split left and right
            splitter = QSplitter(SPLIT_H)
            splitter.addWidget(left_frame)
            splitter.addWidget(self.Stack)
            splitter.setStretchFactor(1, 10)

            # add to widget
            tab_layout.addWidget(splitter)
            main_layout.addLayout(tab_layout, 1)

            #draw left frame
            self.draw_left_frame(left_frame)
            self.draw_right_frame(right_frame)
            self.draw_right_frame2(right_frame2)

            self.setLayout(main_layout)

        except Exception as e:
            traceback.print_exc()
            err = sys.exc_info()[1]
            LOG_write_log(LOG_ERROR, str(err))

левая рамка

def draw_left_frame(self, frame):
        try:
            main_layout = QVBoxLayout(frame)

            confirm_btn = QPushButton(n_this.lb.ENTER, self)
            confirm_btn.setFixedWidth(60)
            main_layout.addWidget(confirm_btn, 0)    
            confirm_btn.clicked.connect(lambda:self.confirm(data))

            main_layout.setAlignment(ALIGN_TOP)

        except Exception as e:
            traceback.print_exc()
            err = sys.exc_info()[1]
            LOG_write_log(LOG_ERROR, str(err))

@pyqtSlot()
def TAB_VIEW_quote_search_code(self, req_quote):
        self.draw_right_frame2(self.right_frame) #change right frame
Задан 22/04/2018 в 06:23
источник пользователем
На других языках...                            


1 ответов

голоса
0

QStackedWidget.setCurrentIndex (индекс) Это свойство содержит индекс виджета, который виден.

Попробуй:

from PyQt5.QtWidgets import *
from PyQt5.QtCore    import *

class Widget(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)

        self.currentWidgetRightFrame = 0
        self.draw_layout()

    def draw_layout(self):
        main_layout = QVBoxLayout(self) 
        #create frame of main tab 
        tab_layout = QHBoxLayout()

        left_frame   = QFrame(self)
        lbLeft = QLabel(" left_frame   ", left_frame) 
        right_frame  = QFrame(self)
        lbRight1 = QLabel(" 1 right_frame", right_frame)  
        right_frame2 = QFrame(self) 
        lbRight2 = QLabel(" 2 right_frame", right_frame2) 

        self.Stack = QStackedWidget(self)
        self.Stack.addWidget(right_frame)
        self.Stack.addWidget(right_frame2)

        left_frame.setFrameShape(QFrame.StyledPanel)
        right_frame.setFrameShape(QFrame.StyledPanel)
        right_frame2.setFrameShape(QFrame.StyledPanel)

        self.right_frame = right_frame2

        # split left and right
        #splitter = QSplitter(SPLIT_H)  
        splitter = QSplitter(self)
        splitter.addWidget(left_frame)
        splitter.addWidget(self.Stack)       
        splitter.setStretchFactor(1, 10)

        # add to widget
        tab_layout.addWidget(splitter)
        main_layout.addLayout(tab_layout, 1)

        #draw left frame
        self.draw_left_frame(left_frame)
        self.setLayout(main_layout)

    def draw_left_frame(self, frame):
        main_layout = QVBoxLayout(frame)

        confirm_btn = QPushButton("switch right_frame", self)      
        confirm_btn.setFixedWidth(100)
        main_layout.addWidget(confirm_btn, 0)    
        #confirm_btn.clicked.connect(lambda:self.confirm(frame))
        confirm_btn.clicked.connect(self.confirm)

        #main_layout.setAlignment(ALIGN_TOP)


    @pyqtSlot()
    #def TAB_VIEW_quote_search_code(self, req_quote):
    def confirm(self): #, req_quote):
        if self.currentWidgetRightFrame == 0:
            self.Stack.setCurrentIndex(1)
            self.currentWidgetRightFrame = 1
        else:
            self.Stack.setCurrentIndex(0)
            self.currentWidgetRightFrame = 0        


if __name__ == "__main__":
    import sys  
    app = QApplication(sys.argv)
    app.setStyle("fusion")
    w = Widget()
    w.show()
    sys.exit(app.exec_())            

введите описание изображения здесь

Ответил 22/04/2018 в 08:42
источник пользователем

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more