Python:日期时间增量可暂停 GUI

Python: datetime delta pauseable GUI

我正在尝试向我的 GUI 添加暂停功能。但是当我取消暂停时,timedelta 会“匆匆”回到原来的时间。我试过 .pause() 但无济于事,我试过 Clock.unschedule(begin_cd)timestamp 它所在的功能,但它仍然只是冲刺回到原来的时间。甚至可以暂停日期时间增量吗? python 文件:

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 文件:

<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
                
        

我发现了两个问题:

  1. 你应该数一数它暂停了多长时间,并在未暂停时进行更正。
self.delta += (self.pause_end - self.pause_start)
  1. 它必须检查当前时间并在 begin_cd
  2. 中取消时钟

完整的工作代码 - 有一些较小的更改

我使用名称 self.end_time 而不是 self.delta 因为它表示 更好的是这个变量中的内容。

我使用正则表达式检查文本输入是否正确 '\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()