PySide如何判断是否点击了按钮?

时间:2013-01-09 05:13:04

标签: python qt pyside

我有一个带有5个按钮的布局,我将其作为“菜单”,因此您单击一个按钮,将显示一个视图,您单击另一个按钮,另一个视图显示。我需要找出点击了哪个按钮,这样我就能根据按下哪个按钮做一些事情。像

这样的东西
if button1_is_clicked:
    do_something()
else:
    do_something_else()

最好的方法是什么? 这是我的代码: 我希望能够更改按钮的样式表,因此处于活动状态和非活动状态

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_())

1 个答案:

答案 0 :(得分:2)

我建议你学习Qt的基础知识,以熟悉信号和插槽。

您需要使初始可见的QPushButton可检查(否则“显示”按钮仅在按住按钮时显示),并将toggled(bool)信号连接到setVisible(bool)你想要'揭示'的按钮的插槽。显然,对于最初不可见的按钮,您必须在实例化时调用setVisible(false)

还有其他更可重复的方法可以达到同样的效果 - 但这会让你开始。