#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__();
self.initUI()
def initUI(self):
self.button = QtGui.QPushButton("print clicked",self)
self.clicked='not_clicked'
self.button.clicked.connect(lambda opt='clicked': self.option(opt))
def option(self,opt):
self.clicked=opt
print opt
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
if __name__=='__main__':
main()
Consider this code. Now, when I click the ‘print clicked’ button. ‘False’ get printed( in the option function). Why is this happening?
QPushButtoninherits the signalclickedfromQAbstractButton.Qt documentation states:
The argument the slot received indicates whether the button is checked. Since
QPushButtonis by default not checkable. the argument is alwaysFalse. That is why ‘False’ got printed.If you want the print result varies, printing ‘True’ or ‘False’, you can either set the button
checkableor change it toQCheckBox.For example,
initUIshould beor