<div dir="ltr"><div>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?</div>
<div> </div><div>Thanks in advance</div><div> </div><div>-Harry</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/7/31 David Hoese <span dir="ltr"><<a href="mailto:dhoese@gmail.com" target="_blank">dhoese@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Harry,<br>
<br>
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: <a href="http://docs.python.org/2/library/functools.html#functools.partial" target="_blank">http://docs.python.org/2/<u></u>library/functools.html#<u></u>functools.partial</a><br>
<br>
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.<br>
<br>
I think you could also use a QTimer without doing a singleShot, but what you have works.<br>
<br>
Please CC me in any replies. Good luck.<br>
<br>
-Dave<div><div class="h5"><br>
<br>
On 7/31/13 6:00 AM, <a href="mailto:pyqt-request@riverbankcomputing.com" target="_blank">pyqt-request@<u></u>riverbankcomputing.com</a> wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid"><div><div class="h5">
Hi,all, I want to use a button to control when the program start in pyqt4,<br>
in other words, when I press the start button, the program will work. I<br>
wrote some code, but it doesn't work. please help me to correct it. Thanks<br>
in advance.<br>
<br>
Best regards<br>
Harry<br>
<br>
import sys<br>
from PyQt4 import QtGui<br>
from PyQt4 import QtCore<br>
import time<br>
<br>
class Example(QtGui.QWidget):<br>
def __init__(self):<br>
super(Example, self).__init__()<br>
self.initUI()<br>
<br>
def initUI(self):<br>
<br>
nowtime = '0000-00-00 00:00:00'<br>
timeEdit = QtGui.QLabel(str(nowtime),<u></u>self)<br>
timeEdit.resize(timeEdit.<u></u>sizeHint())<br>
timeEdit.move(110,30)<br>
<br>
QtCore.QTimer.singleShot(1000,<u></u>lambda:self.newtime(timeEdit))<br>
<br>
startbtn = QtGui.QPushButton('Start', self)<br>
startbtn.setToolTip('Click it to <b>start</b> the program')<br>
startbtn.clicked.connect(self.<u></u>newtime(timeEdit))<br>
startbtn.resize(startbtn.<u></u>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.<u></u>QCoreApplication.instance().<u></u>quit)<br>
qbtn.resize(qbtn.sizeHint())<br>
qbtn.move(400, 340)<br>
<br>
self.setGeometry(300, 200, 600, 400)<br>
self.setWindowTitle('Battery status')<br>
self.show()<br>
<br>
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,<u></u>lambda:self.newtime(timeEdit))<br>
<br>
def main():<br>
<br>
app = QtGui.QApplication(sys.argv)<br>
ex = Example()<br>
sys.exit(app.exec_())<br>
<br>
if __name__ == '__main__':<br>
main()<br>
<br></div></div>
*when executing the program, there is something wrong:*<br>
**<br>
*Traceback (most recent call last):<div class="im"><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.<u></u>newtime(timeEdit))<br>
TypeError: connect() slot argument should be a callable or a signal, not<br></div>
'NoneType'*<br>
</blockquote>
<br>
</blockquote></div><br></div>