Python: pyQt: set MessageBox default button

8 03 2013

20130308

I use MessageBox in pyQt and want to change default button from Yes to No, let see how to do.

first I want to show the basic command to show message box

from PyQt4 import QtGui
reply = QtGui.QMessageBox.question(None,'Warning!!','Click [Yes] to do or [No] to skip',QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
 if reply == QtGui.QMessageBox.Yes:
      print 'Yes'
 else:
      print 'No'

output show in image the default button is highlight on Yes button

Image

when we want to change default from Yes to No button we can do like this

from PyQt4 import QtGui
reply = QtGui.QMessageBox.question(None,'Warning!!','Click [Yes] to do or [No] to skip',QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
     print 'Yes'
else:
     print 'No'

the “QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No” is key point first two are what button to show, this word meas show Yes and No button and after comma is what default button now I set No button and the result is
Image


Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: