<div dir="ltr"><div>Hi,all, I want to use a button to control when the program start in pyqt4, in other words, when I press the start button, the program will work. I wrote some code, but it doesn't work. please help me to correct it. Thanks in advance.</div>
<div> </div><div>Best regards</div><div>Harry</div><div> </div><div>import sys<br>from PyQt4 import QtGui<br>from PyQt4 import QtCore<br>import time</div><div><br>class Example(QtGui.QWidget):</div><div> def __init__(self):<br>
super(Example, self).__init__()</div><div> self.initUI()</div><div><br> def initUI(self):</div><div> <br> nowtime = '0000-00-00 00:00:00'<br> timeEdit = QtGui.QLabel(str(nowtime),self)<br>
timeEdit.resize(timeEdit.sizeHint())<br> timeEdit.move(110,30)</div><div> </div><div> QtCore.QTimer.singleShot(1000,lambda:self.newtime(timeEdit))</div><div> </div><div> startbtn = QtGui.QPushButton('Start', self)<br>
startbtn.setToolTip('Click it to <b>start</b> the program')<br> startbtn.clicked.connect(self.newtime(timeEdit))<br> startbtn.resize(startbtn.sizeHint())<br> startbtn.move(200, 340)<br>
<br> qbtn = QtGui.QPushButton('Quit', self)<br> qbtn.setToolTip('Click it and <b>quit</b> the program')<br> qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)<br>
qbtn.resize(qbtn.sizeHint())<br> qbtn.move(400, 340)</div><div> </div><div> self.setGeometry(300, 200, 600, 400)<br> self.setWindowTitle('Battery status') <br> self.show()</div>
<div> </div><div> def newtime(self,timeEdit):<br> nowtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())<br> timeEdit.setText(str(nowtime))<br> QtCore.QTimer.singleShot(1000,lambda:self.newtime(timeEdit))</div>
<div><br>def main():<br> <br> app = QtGui.QApplication(sys.argv)<br> ex = Example()<br> sys.exit(app.exec_())</div><div><br>if __name__ == '__main__':<br> main()</div><div> </div><div><strong>when executing the program, there is something wrong:</strong></div>
<div><strong></strong> </div><div><strong>Traceback (most recent call last):<br> File "C:\Python\calendar.py", line 62, in <module><br> main()<br> File "C:\Python\calendar.py", line 57, in main<br>
ex = Example()<br> File "C:\Python\calendar.py", line 15, in __init__<br> self.initUI()<br> File "C:\Python\calendar.py", line 32, in initUI<br> startbtn.clicked.connect(self.newtime(timeEdit))<br>
TypeError: connect() slot argument should be a callable or a signal, not 'NoneType'</strong></div><div> </div><div> </div><div> </div></div>