Python 如何向QMessageBox中的自定义按钮添加自定义按钮角色
我想使用带有自定义按钮和自定义按钮角色的QMessageBox小部件创建一个python GUI。我已经创建了自定义按钮,但是如何在单击按钮时向按钮添加自定义角色。小部件有标准的按钮角色,但我想定义自定义按钮单击功能 请帮助我正确的代码语法 下面是我的代码:Python 如何向QMessageBox中的自定义按钮添加自定义按钮角色,python,python-3.x,pyqt,pyqt4,qmessagebox,Python,Python 3.x,Pyqt,Pyqt4,Qmessagebox,我想使用带有自定义按钮和自定义按钮角色的QMessageBox小部件创建一个python GUI。我已经创建了自定义按钮,但是如何在单击按钮时向按钮添加自定义角色。小部件有标准的按钮角色,但我想定义自定义按钮单击功能 请帮助我正确的代码语法 下面是我的代码: import sys from PyQt4 import QtGui,QtCore class MYGUI(QtGui.QWidget): def __init__(self): super(MYGUI,self
import sys
from PyQt4 import QtGui,QtCore
class MYGUI(QtGui.QWidget):
def __init__(self):
super(MYGUI,self).__init__()
self.setWindowTitle("GUI")
#widgets:
self.labl=QtGui.QLabel(self)
self.labl.setFont(QtGui.QFont('Calibri', 34))
#Layout:
Layout =QtGui.QVBoxLayout()
Layout.addWidget(self.labl)
Layout.addStretch()
self.setLayout(Layout)
#Actions:
Queries={'Q1':'question 1','Q2':'question2'}
for k,val in Queries.items():
self.Choice=QtGui.QMessageBox()
self.Choice.setIcon(QtGui.QMessageBox.Question)
self.Choice.setWindowTitle(k)
self.Choice.setText(val)
self.Choice.addButton(QtGui.QPushButton('BT1',self))
self.Choice.addButton(QtGui.QPushButton('BT2',self))
self.Choice.addButton(QtGui.QPushButton('BT3',self))
self.Choice.exec_()
self.show()
def main():
app=QtGui.QApplication(sys.argv)
GUI=MYGUI()
sys.exit(app.exec_())
main()
您必须使用
按钮单击的
信号,这将为您提供发出信号的按钮-
self.Choice.addButton('BT1', QtGui.QMessageBox.YesRole)
self.Choice.addButton('BT2', QtGui.QMessageBox.YesRole)
self.Choice.addButton('BT3', QtGui.QMessageBox.YesRole)
self.Choice.buttonClicked.connect(self.onClicked)
self.Choice.exec_()
def onClicked(self, btn):
print(btn.text())
要创建自定义角色,需要什么类型的角色;或者您希望执行的是特定任务?是,执行用户定义函数中指定的任务。例如,单击按钮时,将调用连接的函数(用户定义)。