Home > Blockchain >  Python: datetime delta pauseable GUI
Python: datetime delta pauseable GUI

Time:03-16

I am trying to add a pause function to my GUI. But when i unpause the timedelta "hurries" back to the original time. I've tried to .pause() but to no avail, ive tried Clock.unschedule(begin_cd), and the timestamp the function that it is in but still it just sprints back to the original time. Is it even possible to pause the datetime delta? python file:

from kivy.app import App
from kivy.clock import Clock
from kivy.uix.widget import Widget
from datetime import datetime, timedelta
import re

class MyGrid(Widget):
    running = False
    def start(self):
        cd_time = self.ids.text_input.text
        check = re.findall('[a-zA-Z]', cd_time)

        if cd_time == '' or len(cd_time) != 8 or check:
            self.ids.show.text = 'Enter time'
        elif cd_time == '00:00:00':
            Clock.unschedule(self.begin_cd)
        else:
            h = cd_time[0:2]
            m = cd_time[3:5]
            s = cd_time[6:8]
            h = int(h)
            m = int(m)
            s = int(s)
            self.delta = datetime.now()   timedelta(hours = h, minutes = m, seconds = s)
        if not self.running:
            self.running = True
            Clock.schedule_interval(self.begin_cd, 0.05)
#begin function
    def begin_cd(self, cd_start):
       if self.running:
            delta = self.delta - datetime.now()
            delta = str(delta)
            self.ids.show.text = '0'   delta[0:7]

            if delta[0:7] == '0:00:00':
               '0'   delta[0:7]

#pausefunction trouble
    def pause(self):
        if self.running:
            self.running = False
            self.ids.pausebutton.text = "unpause"
        elif self.ids.pausebutton.text == "unpause":
            self.running = True
            self.ids.pausebutton.text ="pause"
#start function
    def toggle(self):
        self.start()
class MainApp(App):
    def build(self):
        return MyGrid()
if __name__ == '__main__':
    MainApp().run()

kv file:

<MyGrid>
    BoxLayout:
        orientation:"vertical"
        Label:
            id:show
            text: "00:00:00"
            font_size: 20
            
        TextInput:
            id:text_input
            text:'00:00:00'
            halign:"center"
            
        BoxLayout:
            orientation:"horizontal"
            Button:
                text:"start"
                id:button
                on_press: root.toggle()
            Button:
                text:"pause"
                on_press: root.pause()
                id: pausebutton
                
        

CodePudding user response:

I found two problems:

  1. you should count how long it was paused and make correction when it is unpaused.
self.delta  = (self.pause_end - self.pause_start)
  1. it has to check current time and unschedule clock in begin_cd

Full working code - with few smaller changes

I use name self.end_time instead of self.delta because it expresses better what is in this variable.

I check if text input is correct using regex '\d{2}:\d{2}:\d{2}'

from kivy.app import App
from kivy.clock import Clock
from kivy.uix.widget import Widget
from datetime import datetime, timedelta
import re

class MyGrid(Widget):
    running = False
    def start(self):
        cd_time = self.ids.text_input.text.strip() # remove spaces
        correct = re.findall('\d{2}:\d{2}:\d{2}', cd_time)

        if not correct:
            self.ids.show.text = 'Enter time'
        elif cd_time == '00:00:00':
            Clock.unschedule(self.begin_cd)
        else:
            h = cd_time[0:2]
            m = cd_time[3:5]
            s = cd_time[6:8]
            h = int(h)
            m = int(m)
            s = int(s)
            self.end_time = datetime.now()   timedelta(hours=h, minutes=m, seconds=s)
            
            if not self.running:
                self.running = True
                Clock.schedule_interval(self.begin_cd, 0.05)

    def begin_cd(self, cd_start):
       if self.running:
            delta = self.end_time - datetime.now()
            delta = str(delta)
            self.ids.show.text = '0'   delta[0:7]

            if cd_time == '00:00:00':
                Clock.unschedule(self.begin_cd)

    def pause(self):
        if self.running:
            self.running = False
            self.ids.pausebutton.text = "unpause"
            self.pause_start = datetime.now()
            
        elif self.ids.pausebutton.text == "unpause":
            self.running = True
            self.ids.pausebutton.text ="pause"
            self.pause_end = datetime.now()
            self.end_time  = (self.pause_end - self.pause_start)
            
    def toggle(self):
        self.start()
        
class MainApp(App):
    def build(self):
        return MyGrid()
        
if __name__ == '__main__':
    MainApp().run()
  • Related