2013-01-09 128 views
1

我有一个5个按钮的布局,我充当“菜单”,所以你点击一个按钮,一个视图就会出现,你点击另一个按钮,另一个视图出现。我需要找出哪个按钮被点击,所以我可以根据按下哪个按钮来做一些事情。像PySide如何判断按钮是否被点击?

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

回答

2

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

你需要作出最初显示QPushButton小号可检查(否则,“发现”,而该按钮被按下按钮才会出现),以及toggled(bool)信号连接到要“揭露”按钮的setVisible(bool)插槽。很明显,对于最初不可见的按钮,实例化时必须调用setVisible(false)

实现同样效果还有其他更可重用的方法 - 但这会让您开始。

相关问题