Python按事件隐藏标签和按钮

时间:2017-07-22 14:57:11

标签: python-3.x user-interface pyqt5

当有人按下按钮时我想隐藏我的标签和按钮,但我的代码中有问题,我不能访问标签变量,我不知道如何隐藏它可能适用于此代码?:< / p>

setStyleSheet("display: none;")

这是我的代码,当我按下按钮时,python程序说不响应

   import sys
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    from PyQt5 import QtCore
    from PyQt5 import QtGui
    from PyQt5 import QtWidgets

    class WindowInhalt(QWidget):
        def label(self):
            label1 = QtWidgets.QLabel(self)
            label1.setText("Überschrift mit namen des text adventure")
            label1.setStyleSheet("font-size: 18px;color: black;")
            label1.setGeometry(50, 50, 400, 100)
            label1.move(350, 50)

            label2 = QtWidgets.QLabel(self)
            label2.setText("Spielen")
            label2.setStyleSheet("font-size: 18px;color: black;")
            label2.setGeometry(50, 50, 400, 100)
            label2.move(450, 120)
            label2.mousePressEvent = self.spielen

            label3 = QtWidgets.QLabel(self)
            label3.setText("Settings")
            label3.setStyleSheet("font-size: 18px;color: black;")
            label3.setGeometry(50, 50, 400, 100)
            label3.move(450, 200)
            label3.mousePressEvent = self.settings

            label4 = QtWidgets.QLabel(self)
            label4.setText("Credits")
            label4.setStyleSheet("font-size: 18px;color: black;")
            label4.setGeometry(50, 50, 400, 100)
            label4.move(450, 280)
            label4.mousePressEvent = self.credits

        def Button(self):
            QToolTip.setFont(QFont("Arial", 10))
            button = QPushButton("Spiel beenden", self)
            button.setGeometry(50, 50, 150, 50)
            button.setFont(QFont("Arial", 12))
            button.move(820, 420)
            button.setToolTip("<b>Button lel</b>")
            button.clicked.connect(QtCore.QCoreApplication.instance().quit)
            button.clicked.connect(self.gedruekt)
            button.setStyleSheet("background-color: white;")


    class Window(WindowInhalt):
        def __init__(self):
            super().__init__()
            self.initMe()

        def initMe(self):
            WindowInhalt.Button(self)
            WindowInhalt.label(self)
            self.setGeometry(50,50,1000,500)
            self.setWindowTitle("Gui lalal einhorn")
            self.setWindowIcon(QIcon("cookie.png"))
            self.setAutoFillBackground(True)
            self.setStyleSheet("background-color: lightblue;")
            self.move(500, 250)
            self.show()

        def spielen(self, event,):
            print("spielen")
            WindowInhalt.label.label2.setStyleSheet("display:none;")
#here i want to hide the label


        def settings(self, event):
            print("settings")

        def credits(self, event):
            print("credits")

        def gedruekt(self, event):
            print("Er hats getan ;(")

    if __name__ == "__main__":
        app = QApplication(sys.argv)
        w = Window()
        sys.exit(app.exec_())
    else:
        print("Gui not created, because script used at liabary")
也许somone可以帮助我。

EDIT1: 当我这样做的时候

 def spielen(self, event,):
        print("spielen")
        self.label1 = QtWidgets.QLabel(self)
        self.label1.hide()

比我进入控制台的消息spielen但标签仍然存在 当我这样做的时候:

def spielen(self, event,):
    print("spielen")
    self.label1.hide()

比程序崩溃

EDIT2: 对于任何超过正确代码的人来说,她就是:

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtWidgets

class WindowInhalt(QWidget):
    def label(self):
        self.label1 = QtWidgets.QLabel(self)
        self.label1.setText("Überschrift mit namen des text adventure")
        self.label1.setStyleSheet("font-size: 18px;color: black;")
        self.label1.setGeometry(50, 50, 400, 100)
        self.label1.move(350, 50)
        self.label1.show()

        self.label2 = QtWidgets.QLabel(self)
        self.label2.setText("Spielen")
        self.label2.setStyleSheet("font-size: 18px;color: black;")
        self.label2.setGeometry(50, 50, 400, 100)
        self.label2.move(450, 120)
        self.label2.mousePressEvent = self.spielen
        self.label2.show()

        self.label3 = QtWidgets.QLabel(self)
        self.label3.setText("Settings")
        self.label3.setStyleSheet("font-size: 18px;color: black;")
        self.label3.setGeometry(50, 50, 400, 100)
        self.label3.move(450, 200)
        self.label3.mousePressEvent = self.settings
        self.label3.show()

        self.label4 = QtWidgets.QLabel(self)
        self.label4.setText("Credits")
        self.label4.setStyleSheet("font-size: 18px;color: black;")
        self.label4.setGeometry(50, 50, 400, 100)
        self.label4.move(450, 280)
        self.label4.mousePressEvent = self.credits
        self.label4.show()

    def button(self):
        QToolTip.setFont(QFont("Arial", 10))
        self.button = QPushButton("Spiel beenden", self)
        self.button.setGeometry(50, 50, 150, 50)
        self.button.setFont(QFont("Arial", 12))
        self.button.move(820, 420)
        self.button.setToolTip("<b>Button lel</b>")
        self.button.clicked.connect(QtCore.QCoreApplication.instance().quit)
        self.button.clicked.connect(self.gedruekt)
        self.button.setStyleSheet("background-color: white;")
        self.button.show()


class Window(WindowInhalt):
    def __init__(self):
        super().__init__()
        self.initMe()

    def initMe(self):
        self.label()
        self.button()
        self.setGeometry(50,50,1000,500)
        self.setWindowTitle("Gui lalal einhorn")
        self.setWindowIcon(QIcon("cookie.png"))
        self.setAutoFillBackground(True)
        self.setStyleSheet("background-color: lightblue;")
        self.move(500, 250)
        self.show()

    def spielen(self, event,):
        print("spielen")
        self.label1.hide()

    def settings(self, event):
        print("settings")

    def credits(self, event):
        print("credits")

    def gedruekt(self, event):
        print("Er hats getan ;(")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = Window()
    sys.exit(app.exec_())
else:
    print("Gui not created, because script used at liabary")

1 个答案:

答案 0 :(得分:0)

如果要引用您在类中创建的对象,则需要使用self.labelX将其声明为此类的属性:

self.label1 = QtWidgets.QLabel(self)

然后在这个类的任何方法中,您都可以轻松调用此属性:

self.label1.hide()

或代码中的其他地方

window_inhalt_instance.label1.hide()

编辑: 我专注于你如何实例化课程,你也需要在这里更改一些部分:

标签不会被隐藏,因为它们是使用WindowInhalt.label(self)

创建的

由于您的类Window继承自WindowInhalt,您可以直接调用

 self.label()  # in initme(), same for the buttons

但是你需要通过添加self

来更改WindowInhalt中的每个标签声明