pythonpython-3.xpyqtpyqt5qframe

raise_() function ignored


I am new to python and I am trying to rewrite a GUI with PyQt5. The GUI I made was writen with Tkinter. I am using frames in order to bring up different pages. With Tkinter I succeeded to get pages to the front with tkraise() function. But with PyQt5 it seems as if the function gets ignored.

The file is a test file where I try different things befor adding it to my main file. In the Dothing function I added a print("yes") function to see if it enters the function and it does, but it's not using the raise_() function somehow.

Could anybody explain me what I did wrong or maybe send me a link adress for more information so I search myself. I have already looked on the website of QT and other forums but I couldn't find a answer.

My file looks like this:

import sys
from PyQt5.QtWidgets import (QWidget, QGridLayout, QPushButton, QApplication, QFrame, QLabel, QColorDialog)
from PyQt5.QtGui import (QColor)

Lijst = ["Ferri", "Yvonne", "Ineke"] # , "Sidneger", "Deniel", "Tobie", "Nicol"

class Test(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__()
        self.List = [(255,0,0), (0,255,0), (0,0,255)]
        # self.List = Lijst
        # container = QFrame(self)
        self.setGeometry(300,300,300,300)

        self.Frames = {}
        for F in (self.List):
            selected_color = QColor(F[0],F[1],F[2])
            self.frame = QFrame(self)
            self.frame.setStyleSheet("QWidget { background-color: %s}" % selected_color.name())
            self.frame.setGeometry(100, 0, 300, 300)
            Framas = Pages(self.frame, self, selected_color)
            self.Frames[F] = Framas

    def DoThing(self):
        self.Frames[(255, 0, 0)].raise_()
        print("yes")
        pass

    def DoThing2(self):
        self.Frames[(0, 255, 0)].raise_()
        pass

    def DoThing3(self):
        self.Frames[(0, 0, 255)].raise_()
        pass


class Pages(QFrame):
    def __init__(self, parent, controller, selected_color, *args, **kwargs):
        super().__init__()
        self.controller = controller
        self.frame = parent

        self.button = QPushButton("Change", self.frame) # adding frame as parent
        self.button.move(10, 10)
        self.button.clicked.connect(self.controller.DoThing)

        self.button2 = QPushButton("Change2", self.frame)
        self.button2.move(10, 50)
        self.button2.clicked.connect(self.controller.DoThing2)

        self.button3 = QPushButton("Change3", self.frame)
        self.button3.move(10, 90)
        self.button3.clicked.connect(self.controller.DoThing3)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    Test_window = Test()
    Test_window.show()
    sys.exit(app.exec_())

Solution

  • Having the source of the original code is difficult to know exactly what you require. So I'll try to help you with what I can.

    First you must use only one QFrame, you for each iteration are creating 2: the QFrame that you set the color and the other the Page.

    Secondly you are using raise_() on the Page since you add the page to the dictionary, but the Page does not have the color, but the other QFrame.

    import sys
    from PyQt5.QtWidgets import (QWidget, QPushButton, QApplication, QFrame)
    from PyQt5.QtGui import (QColor)
    
    class Test(QWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
            self.setGeometry(300,300,300,300)
    
            colors = [(255,0,0), (0,255,0), (0,0,255)]
            self.Frames = {}
            for F in colors:
                selected_color = QColor(*F)
                frame = Pages(parent=self, controller=self)
                frame.setStyleSheet("QWidget { background-color: %s}" % selected_color.name())
                frame.setGeometry(100, 0, 300, 300)
                self.Frames[F] = frame
    
        def DoThing(self):
            self.Frames[(255, 0, 0)].raise_()
    
        def DoThing2(self):
            self.Frames[(0, 255, 0)].raise_()
    
        def DoThing3(self):
            self.Frames[(0, 0, 255)].raise_()
    
    class Pages(QFrame):
        def __init__(self, parent, controller):
            super().__init__(parent)
            self.controller = controller
    
            self.button = QPushButton("Change", self) # adding frame as parent
            self.button.move(10, 10)
            self.button.clicked.connect(self.controller.DoThing)
    
            self.button2 = QPushButton("Change2", self)
            self.button2.move(10, 50)
            self.button2.clicked.connect(self.controller.DoThing2)
    
            self.button3 = QPushButton("Change3", self)
            self.button3.move(10, 90)
            self.button3.clicked.connect(self.controller.DoThing3)
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        Test_window = Test()
        Test_window.show()
        sys.exit(app.exec_())