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

David Hoese dhoese at gmail.com
Wed Jul 31 16:14:18 BST 2013


Hi Harry,

There are a couple missing things in your program. First, if you read 
the exception you notice the line of the error and some information 
about what's happening. The message you are getting is saying that the 
argument to "connect" is not callable (meaning a function or method or 
object with a __call__ method). If you look at that line you'll see you 
are calling your "newTime" function and passing what it returns. Instead 
what you want is to pass the function itself. Right above that you use a 
lambda which will work, but I would recommend using the 
functools.partial function: 
http://docs.python.org/2/library/functools.html#functools.partial

You will want to remove the timer call that you have right before 
declaring the button (line 19) otherwise the timer starts before the 
"Start" button is clicked.

I think you could also use a QTimer without doing a singleShot, but what 
you have works.

Please CC me in any replies. Good luck.

-Dave

On 7/31/13 6:00 AM, pyqt-request at riverbankcomputing.com wrote:
> 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'*



More information about the PyQt mailing list