2013-01-09 125 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)

實現同樣效果還有其他更可重用的方法 - 但這會讓您開始。

相關問題