首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PySide如何判断某个按钮是否已被点击?

问PySide如何判断某个按钮是否已被点击?
EN

Stack Overflow用户
提问于 2013-01-09 13:13:04
回答 1查看 2.3K关注 0票数 1

我需要找出哪个按钮被点击了,这样我就可以根据哪个按钮被按下来做一些事情。就像这样

代码语言:javascript
复制
if button1_is_clicked:
    do_something()
else:
    do_something_else()

实现这一目标的最佳方式是什么?以下是我的代码:我希望能够更改按钮的样式表,以便更改活动状态和非活动状态

代码语言:javascript
复制
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_())
EN

回答 1

Stack Overflow用户

发布于 2013-01-09 16:14:33

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

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

还有其他更具可重用性的方法可以达到同样的效果--但这将是你开始的第一步。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14228650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档