PyQT5按钮单击以基于布尔值删除标签

时间:2018-03-06 21:37:51

标签: python pyqt pyqt5 qpushbutton

我想要做的是基于“如果点击一个按钮”的想法,我的小应用程序的“显示区域”它将绘制额外的标签按钮检查按钮,并根据其他按钮删除这些被推了。问题是,这将显示我的新标签,但它不会删除它,它实际上会使文本更暗。

因此...

if button1 == True:显示其他小部件 else:隐藏这些额外的小部件

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton, QLabel
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import pyqtSlot


class App(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = 'test app'
        self.left = 0
        self.top = 0
        self.width = 800
        self.height = 400
        self.statusBarMessage = "Status Bar Test"
        self.currentSprite = 'test.png'
        self.btn1Active = False
        self.btn2Active = False
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.statusBar().showMessage(self.statusBarMessage)

        # Set up the buttons
        btn1 = QPushButton('Set Up', self)
        btn1.setToolTip('Click me for an initial setup')
        btn1.resize(100,100)
        btn1.move(0,0)
        btn1.clicked.connect(self.btn1_clicked)

        btn2 = QPushButton('Set Up', self)
        btn2.setToolTip('Click me for an initial setup')
        btn2.resize(100,100)
        btn2.move(100,0)
        btn2.clicked.connect(self.btn2_clicked)

        spriteHelper = QLabel(self)
        spriteHelper.move(600,100)
        spriteHelper.setPixmap(QPixmap(self.currentSprite))
        spriteHelper.resize(200,280)

        #Display the area
        self.show()
        # I've tried moving this around in different areas without any luck
        self.testLabel = QLabel(self)

    @pyqtSlot()
    def btn1_clicked(self):
        print("Button 1 is clicked")
        self.btn1Active = True
        self.btn2Active = False
        self.currentSprite = 'win_blank_frown.png'
        print(self.btn1Active)
        self.statusBarMessage = "Button 1 was clicked"
        self.statusBar().showMessage(self.statusBarMessage)
        print(self.btn2Active)

        # If the button was clicked and the boolean is true 
        # then create the label
        if self.btn1Active == True:
            self.testLabel.move(400,200)
            self.testLabel.setText("This is text")
            self.testLabel.show()
        # Else if an other button was pushed, set btn1Active to false
        # and remove the label from the main window
        else:
            layout.removeWidget(self.testLabel)
            self.testLabel.deleteLater()
            self.testLabel = None

    def btn2_clicked(self):
        print("Button 2 is clicked")
        self.btn2Active = True
        self.btn1Active = False
        self.currentSprite = 'test.png'
        self.statusBarMessage = "Button 2 was clicked"
        self.statusBar().showMessage(self.statusBarMessage)
        print(self.btn2Active)
        print(self.btn1Active)

0 个答案:

没有答案