B
B
Boris Titarenko2016-09-17 13:45:17
Python
Boris Titarenko, 2016-09-17 13:45:17

Time management in Python?

Help a beginner. how to make a certain operation repeat after a certain number of seconds and at the same time you can do other operations, for example, if there is a status bar and 1% disappears every second, and 2% is added with each button press. I tried through sleep, but you understand. in theory, all the time the decrease should go automatically, and the addition at the touch of a button. came out something like

import sys
import time
from PyQt4 import QtGui
from PyQt4 import QtCore
class Rybalka(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setGeometry(200, 200, 400, 450)
        self.setWindowTitle('')
        self.hp = 10
        self.start = QtGui.QPushButton('Start', self)
        self.start.setGeometry(290, 280, 100, 40)
        self.connect(self.start,QtCore.SIGNAL("clicked()"),self.Timed)
        self.video = QtGui.QPushButton('++', self)
        self.video.setGeometry(10, 80, 100, 40)
        self.connect(self.video,QtCore.SIGNAL("clicked()"),self.Videod)
        self.follow = QtGui.QProgressBar(self)
        self.follow.setGeometry(10,20,370,30)
        self.follow.setMaximum(100)
        self.follow.setMinimum(0)
        self.follow.setValue(self.hp)
    def Timed(self):
        while True: 
            time.sleep(3)
            self.hp -=1
            self.follow.setValue(self.hp)
            if (self.hp < 0):
                break
    def Videod(self):
        self.hp +=2
        self.follow.setValue(self.hp)
app = QtGui.QApplication(sys.argv)
icon = Rybalka()
icon.show()
app.exec_()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2016-09-17
@enotpoloskun23

As Sergey6661313 said you need to use QTimer. The QTimer class emits a timeout signal after the specified time.
Here is an example using QTimer with QProgressBar. The timer emits a timeout signal every second, the on_timer method is called on the signal, in which the QProgressBar is incremented, after the progress is filled, the timer stops.

from PyQt4 import QtGui
from PyQt4 import QtCore

class Widget(QtGui.QWidget):
    
    def __init__(self, parent=None):
        super(Widget, self).__init__()
        layout = QtGui.QVBoxLayout(self)
        self.pb = QtGui.QProgressBar()
        self.pb.setMaximum(100)
        self.pb.setMinimum(0)
        self.pb.setValue(0)
        layout.addWidget(self.pb)
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.on_timer)
        self.timer.start(1000)

    def on_timer(self):
        current_value = self.pb.value() + 1
        self.pb.setValue(current_value)
        if current_value == 100:
            self.timer.stop()

if __name__ == '__main__':
    app = QtGui.QApplication([])
    w = Widget()
    w.show()
    app.exec()

A
Alexey Cheremisin, 2016-09-17
@leahch

Your program is running in one thread. And you need to use either threads or some asynchronous framework. Since you are using QT, it probably has functions for timers, look there.

import sys

from PyQt4.QtCore import QTimer
from PyQt4.QtGui import QApplication

app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)

def tick():
    print 'tick'

timer = QTimer()
timer.timeout.connect(tick)
timer.start(1000)

# run event loop so python doesn't exit
app.exec_()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question