user1960118
user1960118

Reputation: 369

PySide how to tell if a push button has been clicked?

I have a layout with 5 buttons which I act as "menus", so you click on one button and one view will show up, you click another button and another view shows up. I need to find out which button is clicked so I can do something based on which button is pressed. Something like

if button1_is_clicked:
    do_something()
else:
    do_something_else()

What would be the best way to approach this? Here is my code: I want to be able to change the stylesheet of the button, so an active state and a non-active state

from PySide import QtCore
from PySide import QtGui
import VulcanGui

#--------------------------------------------------------------------------
class Program(QtGui.QMainWindow, VulcanGui.Ui_MainWindow):
    def __init__(self, parent=None):
        """ Initialize and setup the User Interface """
        super(Program, self).__init__(parent)
        self.setupUi(self)

        """ Populate the Main Area """
        self.mainArea.setHtml(self.intro_text())


        """ Button Signal/Slots """
        self.introButton.toggled.connect(self.intro_area)
        self.runVulcanButton.clicked.connect(self.vulcan_run_area)
        self.vulcanLogButton.clicked.connect(self.vulcan_log_area)
        self.hostFileButton.clicked.connect(self.edit_host_area)
        self.configEditButton.clicked.connect(self.edit_config_area)            



    def intro_text(self):
        content_file = open("../content/intro_text.html").read()
        return content_file

    '''
    Get the content to print
    '''
    def intro_area(self):
        content_file = open("../content/intro_text.html").read()
        self.mainArea.setHtml(content_file)


    '''
    Function that will display the data when the 'Run Vulcan' button is pressed
    '''
    def vulcan_run_area(self):
        self.mainArea.setPlainText("Button Two ")


    '''
    Function that will display the data when the 'Vulcan Log' button is pressed
    '''
    def vulcan_log_area(self):
        self.mainArea.setPlainText("Button Three")


    '''
    Function that will display the data when the 'Edit Host File' button is pressed
    '''
    def edit_host_area(self):
        self.mainArea.setPlainText("Button Four")

    '''
    Function that will display the data when the 'Edit Config File' button is pressed
    '''
    def edit_config_area(self):
        self.mainArea.setPlainText("Button Five")



#--------------------------------------------------------------------------



if __name__ == "__main__":

    import sys


    program = QtGui.QApplication(sys.argv)
    mWindow = Program()
    mWindow.show()
    sys.exit(program.exec_())

Upvotes: 1

Views: 3404

Answers (1)

cmannett85
cmannett85

Reputation: 22366

I suggest you learn the basics of Qt to get acquainted with signals and slots.

You need to make the initially visible QPushButtons checkable (otherwise the 'revealed' buttons will only appear whilst the button is held down), and connect the toggled(bool) signal to the setVisible(bool) slot of the buttons you want to 'reveal'. Obviously for the buttons that are initially invisible, you would have to call setVisible(false) upon instantiation.

There are other, more reusable, ways of achieving the same effect - but this will get you started.

Upvotes: 2

Related Questions