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

发布于 2025-01-14 07:01:25 字数 2450 浏览 1 评论 0原文

我正在尝试向我的 GUI 添加暂停功能。但是当我取消暂停时,时间增量“匆忙”回到原来的时间。我尝试过 .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
                
        

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
                
        

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍风燃霜 2025-01-21 07:01:25

我发现两个问题:

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

完整的工作代码 - 进行一些较小的更改,

我使用名称 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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文