PyQt5raise_()函数被忽略

时间:2018-07-12 20:42:57

标签: python-3.x pyqt5

我是python的新手,我正在尝试使用PyQt5重写GUI。我制作的GUI是用Tkinter编写的。我正在使用 frames 来显示不同的页面。使用Tkinter,我成功地使用tkraise()函数将页面置于最前面。但是使用PyQt5似乎该函数被忽略了。

该文件是一个测试文件,在将其添加到主文件之前,我会尝试其他操作。 在 Dothing 函数中,我添加了一个print(“ yes”)函数,以查看它是否已进入该函数并且确实存在,但它不以某种方式使用 raise _()函数。

任何人都可以向我解释我做错了什么,或者向我发送链接地址以获取更多信息,以便我进行自我搜索。我已经看过QT网站和其他论坛,但找不到答案。

我的文件如下:

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_())

1 个答案:

答案 0 :(得分:0)

拥有原始代码的来源很难准确地知道您的要求。因此,我会尽力帮助您。

首先,您必须只使用一个QFrame,每次迭代都将创建2:设置颜色的QFrame和另一个Page。

其次,因为您将页面添加到字典中,所以您正在页面上使用raise_(),但是页面没有颜色,而其他颜色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_())