首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >忽略raise_()函数

忽略raise_()函数
EN

Stack Overflow用户
提问于 2018-07-12 20:42:57
回答 1查看 1.7K关注 0票数 2

我对python很陌生,我正在尝试用PyQt5重写一个GUI。我制作的GUI是用Tkinter编写的。我正在使用框架来显示不同的页面。使用Tkinter,我成功地使用tkraise()函数将页面放到了最前面。但是使用PyQt5,函数似乎被忽略了。

该文件是一个测试文件,在将它添加到我的主文件之前,我尝试了不同的东西。在Dothing函数中,我添加了print("yes")函数,以查看它是否进入该函数,但它没有以某种方式使用raise_()函数。

有谁能解释我做错了什么,或者给我发送一个链接,以获取更多的信息,所以我自己搜索。我已经看了QT和其他论坛的网站,但我找不到答案。

我的档案是这样的:

代码语言:javascript
复制
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_())
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-13 00:01:54

拥有原始代码的源代码很难准确地知道您需要什么。所以我会尽力帮你。

首先,您必须只使用一个QFrame,每次迭代都要创建两个:设置颜色的QFrame,而另一个是页面。

其次,您在页面上使用raise_(),因为您将页面添加到字典中,但是页面没有颜色,而是另一个QFrame

代码语言:javascript
复制
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_())
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51314138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档