1

I want to create a python GUI using QMessageBox Widget with custom buttons and custom button roles. I have created custom buttons but how to add custom role to a button when it is clicked. The widget has standard button roles but I want to define custom button click functions.

Kindly help me with proper code syntax.

Below is my code:

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()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
pavan sunder
  • 99
  • 4
  • 15

1 Answers1

4

You must use the buttonClicked signal, this gives you the button that emits the signal-

    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())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241