1

我有一个带有 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_())
4

1 回答 1

2

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

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

还有其他更可重用的方法来实现相同的效果——但这会让你开始。

于 2013-01-09T08:14:32.613 回答