PyQt - QPushButton Widget



In any GUI design, the command button is the most important and most often used control. Buttons with Save, Open, OK, Yes, No and Cancel etc. as caption are familiar to any computer user. In PyQt API, the QPushButton class object presents a button which when clicked can be programmed to invoke a certain function.

QPushButton class inherits its core functionality from QAbstractButton class. It is rectangular in shape and a text caption or icon can be displayed on its face.

Following are some of the most commonly used methods of QPushButton class −

Sr.No. Methods & Description
1

setCheckable()

Recognizes pressed and released states of button if set to true

2

toggle()

Toggles between checkable states

3

setIcon()

Shows an icon formed out of pixmap of an image file

4

setEnabled()

When set to false, the button becomes disabled, hence clicking it doesn’t emit a signal

5

isChecked()

Returns Boolean state of button

6

setDefault()

Sets the button as default

7

setText()

Programmatically sets buttons’ caption

8

text()

Retrieves buttons’ caption

Example

Four QPushButton objects are set with some of the above attributes. The example is written in object oriented form, because the source of the event is needed to be passed as an argument to slot function.

Four QPushButton objects are defined as instance variables in the class. First button b1 is converted into toggle button by the statements −

self.b1.setCheckable(True)
self.b1.toggle()

Clicked signal of this button is connected to a member method btnstate() which identifies whether button is pressed or released by checking isChecked() property.

def btnstate(self):
   if self.b1.isChecked():
      print "button pressed"
   else:
      print "button released"

Second button b2 displays an icon on the face. setIcon() method takes a pixmap object of any image file as argument.

b2.setIcon(QIcon(QPixmap("python.gif")))

Button b3 is set to be disabled by using setEnabled() method −

b3.setEnabled(False)

PushButton b4 is set to default button by setDefault() method. Shortcut to its caption is created by prefixing & to the caption (&Default). As a result, by using the keyboard combination Alt+D, connected slot method will be called.

Buttons b1 and b4 are connected to whichbtn() slot method. Since the function is intended to retrieve caption of the clicked button, the button object should be passed as an argument. This is achieved by the use of lambda function.

For example,

b4.clicked.connect(lambda:self.whichbtn(self.b4))

The complete code is given below −

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Form(QDialog):
   def __init__(self, parent=None):
      super(Form, self).__init__(parent)
		
      layout = QVBoxLayout()
      self.b1 = QPushButton("Button1")
      self.b1.setCheckable(True)
      self.b1.toggle()
      self.b1.clicked.connect(lambda:self.whichbtn(self.b1))
      self.b1.clicked.connect(self.btnstate)
      layout.addWidget(self.b1)
		
      self.b2 = QPushButton()
      self.b2.setIcon(QIcon(QPixmap("python.gif")))
      self.b2.clicked.connect(lambda:self.whichbtn(self.b2))
      layout.addWidget(self.b2)
      self.setLayout(layout)
      self.b3 = QPushButton("Disabled")
      self.b3.setEnabled(False)
      layout.addWidget(self.b3)
		
      self.b4 = QPushButton("&Default")
      self.b4.setDefault(True)
      self.b4.clicked.connect(lambda:self.whichbtn(self.b4))
      layout.addWidget(self.b4)
      
      self.setWindowTitle("Button demo")

   def btnstate(self):
      if self.b1.isChecked():
         print "button pressed"
      else:
         print "button released"
			
   def whichbtn(self,b):
      print "clicked button is "+b.text()

def main():
   app = QApplication(sys.argv)
   ex = Form()
   ex.show()
   sys.exit(app.exec_())
	
if __name__ == '__main__':
   main()

The above code produces the following output.

QPushButton Widget Output
clicked button is Button1
button released
clicked button is Button1
button pressed
clicked button is &Default
Advertisements