[PyQt] how to use the button to control the program

吉文 jiwen0304 at gmail.com
Wed Jul 31 00:27:05 BST 2013


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.

Best regards
Harry

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
import time

class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):

        nowtime = '0000-00-00 00:00:00'
        timeEdit = QtGui.QLabel(str(nowtime),self)
        timeEdit.resize(timeEdit.sizeHint())
        timeEdit.move(110,30)

        QtCore.QTimer.singleShot(1000,lambda:self.newtime(timeEdit))

        startbtn = QtGui.QPushButton('Start', self)
        startbtn.setToolTip('Click it to <b>start</b> the program')
        startbtn.clicked.connect(self.newtime(timeEdit))
        startbtn.resize(startbtn.sizeHint())
        startbtn.move(200, 340)

        qbtn = QtGui.QPushButton('Quit', self)
        qbtn.setToolTip('Click it and <b>quit</b> the program')
        qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        qbtn.resize(qbtn.sizeHint())
        qbtn.move(400, 340)

        self.setGeometry(300, 200, 600, 400)
        self.setWindowTitle('Battery status')
        self.show()

    def newtime(self,timeEdit):
        nowtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
        timeEdit.setText(str(nowtime))
        QtCore.QTimer.singleShot(1000,lambda:self.newtime(timeEdit))

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

*when executing the program, there is something wrong:*
**
*Traceback (most recent call last):
  File "C:\Python\calendar.py", line 62, in <module>
    main()
  File "C:\Python\calendar.py", line 57, in main
    ex = Example()
  File "C:\Python\calendar.py", line 15, in __init__
    self.initUI()
  File "C:\Python\calendar.py", line 32, in initUI
    startbtn.clicked.connect(self.newtime(timeEdit))
TypeError: connect() slot argument should be a callable or a signal, not
'NoneType'*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20130731/d5ff8914/attachment.html>


More information about the PyQt mailing list