[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.
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
import time
import functools

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

    def initUI(self):

        startbtn = QtGui.QPushButton('Start', self)
        startbtn.setToolTip('Click it to <b>start</b> the program')
        startbtn.clicked.connect(self.newtime)
        startbtn.resize(startbtn.sizeHint())
        startbtn.move(200, 340)
        nowtime = '0000-00-00 00:00:00'
        self.timeEdit = QtGui.QLabel(str(nowtime),self)
        self.timeEdit.move(110,30)

        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):
        while True:
            nowtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
            self.timeEdit.setText(str(nowtime))
def main():

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

if __name__ == '__main__':
    main()

If I remove 'while True:',it can work, but only show the local time, how to
fix the loop.

Thank you in advance.
Best regards
Harry


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
> example.
>
> 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:
>             timeEdit.setText(str(i))
>             i += 1
>
> to:
>
>     def newtime(self):
>         timeEdit.setText(str(self.i))
>         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/*
> *2010/06/17/youre-doing-it-**wrong/<http://blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong/>
> 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.
>
> -Dave
>
>
> 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
>> -Harry
>>
>>
>> 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:
>>     http://docs.python.org/2/__**library/functools.html#__**
>> functools.partial<http://docs.python.org/2/__library/functools.html#__functools.partial><
>> 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 at __riverbankcomput**ing.com<http://riverbankcomputing.com>
>>
>>     <mailto: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/20130808/f35a87be/attachment-0001.html>


More information about the PyQt mailing list