-1
我想隱藏我的標籤和按鈕,當有人按下按鈕,但我有我的代碼中的問題,我不能訪問標籤變量,我不知道如何隱藏也許它與此代碼?:Python隱藏標籤和事件按鈕
setStyleSheet("display: none;")
這裏工作是我的代碼,不工作我磨片按下按鈕蟒蛇PROGRAMM說不迴應
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")
也許有人爲能幫助我。
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: 對於任何人誰是正確的代碼上面instredtef她,他是:
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")
好吧,我試試謝謝 – Yuto
是啊,現在它的工作原理非常感謝我還是一個初學者 – Yuto
@Yuto沒有概率,想想當你創建一個類時,它是創建對象的模型,不要直接使用這個模型作爲'WindowInhalt.label(self)',而是創建實例,就像你用'w = Window()'所做的那樣。 – PRMoureu