1

「メニュー」として機能する 5 つのボタンを備えたレイアウトがあるため、1 つのボタンをクリックすると 1 つのビューが表示され、別のボタンをクリックすると別のビューが表示されます。どのボタンが押されたかに基づいて何かを実行できるように、どのボタンがクリックされたかを調べる必要があります。何かのようなもの

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 の基礎を学ぶことをお勧めします。

最初に表示されている s をチェック可能にする必要がありますQPushButton(そうしないと、ボタンが押されている間だけ「表示された」ボタンが表示されます)、「表示」するボタンのスロットにtoggled(bool)信号を接続します。setVisible(bool)明らかに、最初は非表示のボタンについては、setVisible(false)インスタンス化を呼び出す必要があります。

同じ効果を達成するための、より再利用可能な方法は他にもありますが、これで始めることができます。

于 2013-01-09T08:14:32.613 に答える