[PyQt] how to use the button to control the program
jiwen0304 at gmail.com
Thu Aug 8 23:25:27 BST 2013
Hello,Dave, thank you very much for helping me, I am so sorry for replying
later, I did the hardware of my project in these days.
According to your hints, I changed my code, but when I click the start
button, the program is dead, no any response. The following is my code.
from PyQt4 import QtGui
from PyQt4 import QtCore
startbtn = QtGui.QPushButton('Start', self)
startbtn.setToolTip('Click it to <b>start</b> the program')
nowtime = '0000-00-00 00:00:00'
self.timeEdit = QtGui.QLabel(str(nowtime),self)
qbtn = QtGui.QPushButton('Quit', self)
qbtn.setToolTip('Click it and <b>quit</b> the program')
self.setGeometry(300, 200, 600, 400)
nowtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
app = QtGui.QApplication(sys.argv)
ex = Example()
if __name__ == '__main__':
If I remove 'while True:',it can work, but only show the local time, how to
fix the loop.
Thank you in advance.
2013/8/1 David Hoese <dhoese at gmail.com>
> The way a timer works, especially the singleShot you are using, is that
> once it is time to act it calls the function pointer you've provided. You
> *must* provide a callable. Every tick of the timer is just calling that
> callable with no parameters. You could provide a function pointer to a
> function that will never return, but I think this will block your GUI event
> loop (see below). I'd have to test it myself, but don't really have the
> time right now, sorry.
> If I understand you correctly, what you would like to happen is:
> 1. User clicks "Start"
> 2. newtime is called
> 3. newtime "creates" a new value
> 4. The time label (or other widget) gets updated with this new value
> If you are just updating the time based on a certain interval, use a
> QTimer without the singleShot, similar to what you are doing in your
> If updating a time is just an example in the code you've provided and you
> would like to update a value I would suggest still using a QTimer or using
> threads (QThread). It depends on why your "newtime" function needs to be a
> long running loop that never returns.
> If it is a long running loop that never returns because it continuously
> updates the values, then you could change newtime to be "per iteration",
> use QTimers, and call newtime every "tick" of the timer. So go from a
> function like this:
> def newtime(self):
> i = 0
> while True:
> i += 1
> def newtime(self):
> self.i += 1
> If your loop is long running because it blocks on system IO (network
> communications, file reading, database, etc.) then you should use QThreads.
> Any type of long running code that runs in the main/GUI thread will block
> your GUI and the user won't be able to interact. I suggest researching PyQt
> Event loops, QThreads, and after that read http://blog.qt.digia.com/blog/*
> I've posted on this mailing list before helping users with their QThread
> problems, if you look those up they may help.
> Let me know what you decide to do and hopefully this rambling made sense.
> On 7/31/2013 7:10 PM, 吉文 wrote:
>> 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
>> 2013/7/31 David Hoese <dhoese at gmail.com <mailto: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:
>> 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.
>> On 7/31/13 6:00 AM, pyqt-request at __riverbankcomput**ing.com<http://riverbankcomputing.com>
>> <mailto:pyqt-request@**riverbankcomputing.com<pyqt-request at riverbankcomputing.com>>
>> 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
>> import sys
>> from PyQt4 import QtGui
>> from PyQt4 import QtCore
>> import time
>> class Example(QtGui.QWidget):
>> def __init__(self):
>> super(Example, self).__init__()
>> def initUI(self):
>> nowtime = '0000-00-00 00:00:00'
>> timeEdit = QtGui.QLabel(str(nowtime),__**self)
>> startbtn = QtGui.QPushButton('Start', self)
>> startbtn.setToolTip('Click it to <b>start</b> the
>> startbtn.move(200, 340)
>> qbtn = QtGui.QPushButton('Quit', self)
>> qbtn.setToolTip('Click it and <b>quit</b> the program')
>> qbtn.move(400, 340)
>> self.setGeometry(300, 200, 600, 400)
>> self.setWindowTitle('Battery status')
>> def newtime(self,timeEdit):
>> nowtime = time.strftime('%Y-%m-%d %H:%M:%S',
>> def main():
>> app = QtGui.QApplication(sys.argv)
>> ex = Example()
>> if __name__ == '__main__':
>> *when executing the program, there is something wrong:*
>> *Traceback (most recent call last):
>> File "C:\Python\calendar.py", line 62, in <module>
>> File "C:\Python\calendar.py", line 57, in main
>> ex = Example()
>> File "C:\Python\calendar.py", line 15, in __init__
>> File "C:\Python\calendar.py", line 32, in initUI
>> TypeError: connect() slot argument should be a callable or a
>> signal, not
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the PyQt