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

吉文 jiwen0304 at gmail.com
Thu Aug 1 01:10:27 BST 2013


Hi, Dave, thank you very much for helping me. I am a newcomer to pyqt4, so
maybe my questions are low-level.Thanks again. If the function does not
have returns, can the button connect the function? The newtime function
realizes a loop to change the time every 1s, there is no return. I want to
click the 'start' button to begin the function, how to realize that?

Thanks in advance

-Harry


2013/7/31 David Hoese <dhoese at gmail.com>

> 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<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@**riverbankcomputing.com<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'*
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20130801/ca094db4/attachment-0001.html>


More information about the PyQt mailing list