0

I wanted to make a simple stopwatch, so I set up a button to turn on the a timer to update the time label. When I press the button tied to the timer, it doesn't do anything except for print out my start_pause message. I've tried multiple different things, but none of my approaches seemed to have worked. front_basic_timer_ui is the GUI side of the code.

import front_basic_timer_ui
import datetime
import sys

from PyQt4 import QtCore, QtGui

if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)

    def update_label():
        gui.label.setText("update")
    def stop_timer():
        print("stop")
    def save_timer_log():
        print("save")
    def start_pause_timer(self):
        print("start_pause")
        #global set_time
        #set_time = datetime.datetime.today()
        timer = QtCore.QTimer()
        timer.timeout.connect(update_label)
        timer.start(100)
    #Set the window from imported front_basic_timer_ui
    gui_plot = front_basic_timer_ui.QtGui.QMainWindow()
    gui = front_basic_timer_ui.Ui_MainWindow()
    gui.setupUi(gui_plot)

    #Set buttons
    gui.pushButton.clicked.connect(save_timer_log)
    gui.pushButton_2.clicked.connect(stop_timer)
    gui.pushButton_3.clicked.connect(start_pause_timer)

    gui_plot.show()
    sys.exit(app.exec_())
charlee
  • 31
  • 1
  • 8
  • 2
    You're so impatient. You started the timer with a timeout of 100000000 milliseconds, which is 27.7 hours. Come back tomorrow and let us know if the label has changed. – Paul Cornelius Oct 09 '17 at 22:53
  • Nice catch, though as I mentioned before, I've tried a lot of different things. The 100000000 milliseconds thing is not the cause unfortunately. I just reduced the time and the problem still remains. – charlee Oct 09 '17 at 23:46
  • 1
    The timer will just get garbage-collected as soon as `start_pause_timer` returns. Use `global timer`. – ekhumoro Oct 10 '17 at 00:27
  • @ekhumoro I think you're right. Since `timer` is local to `start_pause_timer` it is subject to garbage collection as soon as the function ends. This is an unfortunate general "feature" of Qt on Python. Instead of declaring it global you could just move `timer=QtCore.QTimer()` outside the function. – Paul Cornelius Oct 10 '17 at 00:38
  • @ekhumoro & Paul Cornelius You two are right. I moved it out of the function and it works perfectly. Thanks a lot. – charlee Oct 10 '17 at 01:11
  • @PaulCornelius. It has nothing to do with Qt. This is how Python always behaves. It is just the normal scoping of variables in functions. – ekhumoro Oct 10 '17 at 03:08
  • @ekhumoro. Yes, but the consequences in Qt aren't always obvious. Python Timer objects won't disappear until after they have called their target function. But QTimers disappear if their Python object goes out of scope. I now know that QObjects don't keep references to their Python proxies, but when I was learning Qt this got me a few times. For example I expected that adding a widget to a layout manager should be like just like appending an object to a Python list. But it doesn't behave that way. – Paul Cornelius Oct 10 '17 at 03:44
  • @PaulCornelius. So as far as timers are concerned, Python is the oddball here, rather than Qt. And adding a widget to a layout **does** append: the widgets will be automatically re-parented to whatever widget becomes the parent of the layout. – ekhumoro Oct 10 '17 at 11:55

0 Answers0