Python:日期时间增量可暂停 GUI
我正在尝试向我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现两个问题:
begin_cd
中取消安排时钟完整的工作代码 - 进行一些较小的更改,
我使用名称
self.end_time
而不是self.delta
> 因为它表达了更好地说明这个变量中的内容。
我使用正则表达式
'\d{2}:\d{2}:\d{2}'
检查文本输入是否正确I found two problems:
begin_cd
Full working code - with few smaller changes
I use name
self.end_time
instead ofself.delta
because it expressesbetter what is in this variable.
I check if text input is correct using regex
'\d{2}:\d{2}:\d{2}'