我需要找出哪个按钮被点击了,这样我就可以根据哪个按钮被按下来做一些事情。就像这样
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_())发布于 2013-01-09 16:14:33
我建议你学习Qt的基础知识来熟悉信号和插槽。
您需要使初始可见的QPushButton可选(否则,只有在按住按钮的同时才会出现“显示”按钮),并将toggled(bool)信号连接到您想要“显示”的按钮的setVisible(bool)插槽。显然,对于最初不可见的按钮,必须在实例化时调用setVisible(false)。
还有其他更具可重用性的方法可以达到同样的效果--但这将是你开始的第一步。
https://stackoverflow.com/questions/14228650
复制相似问题