如何在 Kivy 中更新标签文本,Python
How to update text of a label in Kivy, Python
我在Kivy 中制作了一个简单的计时器程序和一个带有标签和按钮的框布局。每当按下按钮时,我都希望计时器启动并且标签显示经过的时间。我遵循了本教程:
https://www.youtube.com/watch?v=cggCobcS3vU
然后在代码中添加了一些我自己的东西,但标签只显示 .kv 文件中设置的初始文本。任何修复?
timer.py
import time
from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import StringProperty, ObjectProperty
class MyApp(BoxLayout):
def __init__(self, **kwargs):
super(MyApp, self).__init__(**kwargs)
self.output = ''
def update_label(self):
self.lbl1.text = str(self.output)
def count(self, *varargs):
timeLoop = True
Sec = 0
Min = 0
Hrs = 0
while timeLoop == True:
self.update_label()
print(str(self.output))
Sec += 1
time.sleep(1)
if Sec == 60:
Sec = 0
Min += 1
if Min == 60:
Min = 0
Hrs += 1
if Sec <= 9 and Min <=9 and Hrs <= 9:
self.output = '0' + str(Hrs) +'.'+ '0' + str(Min) + "." + '0' + str(Sec)
elif Sec <= 9 and Min <=9 and Hrs >=9:
self.output = str(Hrs) + '.'+ '0' + str(Min) + "." + '0' + str(Sec)
elif Sec <= 9 and Min >=9 and Hrs >=9:
self.output = str(Hrs) + '.'+ str(Min) + "." + '0' + str(Sec)
elif Sec >= 9 and Min >=9 and Hrs >=9:
self.output = str(Hrs) + '.'+ str(Min) + "." + str(Sec)
elif Sec >= 9 and Min >=9 and Hrs <=9:
self.output = '0' + str(Hrs) +'.'+ str(Min) + "." + str(Sec)
elif Sec >= 9 and Min <= 9 and Hrs <=9:
self.output = '0' + str(Hrs) +'.'+ '0' + str(Min) + "." + str(Sec)
elif Sec >= 9 and Min <=9 and Hrs >= 9:
self.output = str(Hrs) +'.'+ '0' + str(Min) + "." + str(Sec)
elif Sec <= 9 and Min >= 9 and Hrs <=9:
self.output = '0' + str(Hrs) +'.'+ str(Min) + "." + '0' + str(Sec)
class MainApp(App):
def build(self):
c = MyApp()
return c
if __name__ == '__main__':
MainApp().run()
mainapp.kv
<MyApp>:
lbl1: label1
BoxLayout:
size: root.size
Button:
id: button1
text: "Change text"
on_press: root.count()
Label:
id: label1
text: "hi"
我知道这个:How to change text of a label in the kivy language with python
还有这个:https://groups.google.com/forum/#!topic/kivy-users/mdqPQYBWEU8
但这两个都不适合我。
诸如 time.sleep()
之类的阻塞任务以及无限循环对 GUI 不友好,因为它存在于事件循环中,因此阻塞任务不允许处理其他事件,例如更改 [=26] 的大小=],移动 window,等等
Kivy 可以在不使用 True 或 time.sleep()
的情况下创建周期性任务,为此 Clock
使用:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
class MyApp(BoxLayout):
def __init__(self, **kwargs):
super(MyApp, self).__init__(**kwargs)
self.output = ''
def update_label(self):
self.lbl1.text = str(self.output)
def count(self, *varargs):
self.Sec = 0
self.Min = 0
self.Hrs = 0
Clock.schedule_interval(self.on_timeout, 1)
def on_timeout(self, *args):
self.update_label()
self.Sec += 1
if self.Sec == 60:
self.Sec = 0
self.Min += 1
if self.Min == 60:
self.Min = 0
self.Hrs += 1
if self.Sec <= 9 and self.Min <=9 and self.Hrs <= 9:
self.output = '0' + str(self.Hrs) +'.'+ '0' + str(self.Min) + "." + '0' + str(self.Sec)
elif self.Sec <= 9 and self.Min <=9 and self.Hrs >=9:
self.output = str(self.Hrs) + '.'+ '0' + str(self.Min) + "." + '0' + str(self.Sec)
elif self.Sec <= 9 and self.Min >=9 and self.Hrs >=9:
self.output = str(self.Hrs) + '.'+ str(self.Min) + "." + '0' + str(self.Sec)
elif self.Sec >= 9 and self.Min >=9 and self.Hrs >=9:
self.output = str(self.Hrs) + '.'+ str(self.Min) + "." + str(self.Sec)
elif self.Sec >= 9 and self.Min >=9 and self.Hrs <=9:
self.output = '0' + str(self.Hrs) +'.'+ str(self.Min) + "." + str(self.Sec)
elif self.Sec >= 9 and self.Min <= 9 and self.Hrs <=9:
self.output = '0' + str(self.Hrs) +'.'+ '0' + str(self.Min) + "." + str(self.Sec)
elif self.Sec >= 9 and self.Min <=9 and self.Hrs >= 9:
self.output = str(self.Hrs) +'.'+ '0' + str(self.Min) + "." + str(self.Sec)
elif self.Sec <= 9 and self.Min >= 9 and self.Hrs <=9:
self.output = '0' + str(self.Hrs) +'.'+ str(self.Min) + "." + '0' + str(self.Sec)
class MainApp(App):
def build(self):
c = MyApp()
return c
if __name__ == '__main__':
MainApp().run()
加上:
我们可以使用库来减少它,而不是进行繁琐的计算来获得时间:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from datetime import datetime
class MyApp(BoxLayout):
def count(self, *varargs):
self.start = datetime.now()
Clock.schedule_interval(self.on_timeout, 1)
def on_timeout(self, *args):
d = datetime.now() - self.start
self.lbl1.text = datetime.utcfromtimestamp(d.total_seconds()).strftime("%H.%M.%S")
class MainApp(App):
def build(self):
c = MyApp()
return c
if __name__ == '__main__':
MainApp().run()
在您的 KV 文件中,id 是 label1,但是当您更新时,您更新的是 lbl1 的文本。
我在Kivy 中制作了一个简单的计时器程序和一个带有标签和按钮的框布局。每当按下按钮时,我都希望计时器启动并且标签显示经过的时间。我遵循了本教程: https://www.youtube.com/watch?v=cggCobcS3vU 然后在代码中添加了一些我自己的东西,但标签只显示 .kv 文件中设置的初始文本。任何修复?
timer.py
import time
from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import StringProperty, ObjectProperty
class MyApp(BoxLayout):
def __init__(self, **kwargs):
super(MyApp, self).__init__(**kwargs)
self.output = ''
def update_label(self):
self.lbl1.text = str(self.output)
def count(self, *varargs):
timeLoop = True
Sec = 0
Min = 0
Hrs = 0
while timeLoop == True:
self.update_label()
print(str(self.output))
Sec += 1
time.sleep(1)
if Sec == 60:
Sec = 0
Min += 1
if Min == 60:
Min = 0
Hrs += 1
if Sec <= 9 and Min <=9 and Hrs <= 9:
self.output = '0' + str(Hrs) +'.'+ '0' + str(Min) + "." + '0' + str(Sec)
elif Sec <= 9 and Min <=9 and Hrs >=9:
self.output = str(Hrs) + '.'+ '0' + str(Min) + "." + '0' + str(Sec)
elif Sec <= 9 and Min >=9 and Hrs >=9:
self.output = str(Hrs) + '.'+ str(Min) + "." + '0' + str(Sec)
elif Sec >= 9 and Min >=9 and Hrs >=9:
self.output = str(Hrs) + '.'+ str(Min) + "." + str(Sec)
elif Sec >= 9 and Min >=9 and Hrs <=9:
self.output = '0' + str(Hrs) +'.'+ str(Min) + "." + str(Sec)
elif Sec >= 9 and Min <= 9 and Hrs <=9:
self.output = '0' + str(Hrs) +'.'+ '0' + str(Min) + "." + str(Sec)
elif Sec >= 9 and Min <=9 and Hrs >= 9:
self.output = str(Hrs) +'.'+ '0' + str(Min) + "." + str(Sec)
elif Sec <= 9 and Min >= 9 and Hrs <=9:
self.output = '0' + str(Hrs) +'.'+ str(Min) + "." + '0' + str(Sec)
class MainApp(App):
def build(self):
c = MyApp()
return c
if __name__ == '__main__':
MainApp().run()
mainapp.kv
<MyApp>:
lbl1: label1
BoxLayout:
size: root.size
Button:
id: button1
text: "Change text"
on_press: root.count()
Label:
id: label1
text: "hi"
我知道这个:How to change text of a label in the kivy language with python
还有这个:https://groups.google.com/forum/#!topic/kivy-users/mdqPQYBWEU8
但这两个都不适合我。
诸如 time.sleep()
之类的阻塞任务以及无限循环对 GUI 不友好,因为它存在于事件循环中,因此阻塞任务不允许处理其他事件,例如更改 [=26] 的大小=],移动 window,等等
Kivy 可以在不使用 True 或 time.sleep()
的情况下创建周期性任务,为此 Clock
使用:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
class MyApp(BoxLayout):
def __init__(self, **kwargs):
super(MyApp, self).__init__(**kwargs)
self.output = ''
def update_label(self):
self.lbl1.text = str(self.output)
def count(self, *varargs):
self.Sec = 0
self.Min = 0
self.Hrs = 0
Clock.schedule_interval(self.on_timeout, 1)
def on_timeout(self, *args):
self.update_label()
self.Sec += 1
if self.Sec == 60:
self.Sec = 0
self.Min += 1
if self.Min == 60:
self.Min = 0
self.Hrs += 1
if self.Sec <= 9 and self.Min <=9 and self.Hrs <= 9:
self.output = '0' + str(self.Hrs) +'.'+ '0' + str(self.Min) + "." + '0' + str(self.Sec)
elif self.Sec <= 9 and self.Min <=9 and self.Hrs >=9:
self.output = str(self.Hrs) + '.'+ '0' + str(self.Min) + "." + '0' + str(self.Sec)
elif self.Sec <= 9 and self.Min >=9 and self.Hrs >=9:
self.output = str(self.Hrs) + '.'+ str(self.Min) + "." + '0' + str(self.Sec)
elif self.Sec >= 9 and self.Min >=9 and self.Hrs >=9:
self.output = str(self.Hrs) + '.'+ str(self.Min) + "." + str(self.Sec)
elif self.Sec >= 9 and self.Min >=9 and self.Hrs <=9:
self.output = '0' + str(self.Hrs) +'.'+ str(self.Min) + "." + str(self.Sec)
elif self.Sec >= 9 and self.Min <= 9 and self.Hrs <=9:
self.output = '0' + str(self.Hrs) +'.'+ '0' + str(self.Min) + "." + str(self.Sec)
elif self.Sec >= 9 and self.Min <=9 and self.Hrs >= 9:
self.output = str(self.Hrs) +'.'+ '0' + str(self.Min) + "." + str(self.Sec)
elif self.Sec <= 9 and self.Min >= 9 and self.Hrs <=9:
self.output = '0' + str(self.Hrs) +'.'+ str(self.Min) + "." + '0' + str(self.Sec)
class MainApp(App):
def build(self):
c = MyApp()
return c
if __name__ == '__main__':
MainApp().run()
加上:
我们可以使用库来减少它,而不是进行繁琐的计算来获得时间:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from datetime import datetime
class MyApp(BoxLayout):
def count(self, *varargs):
self.start = datetime.now()
Clock.schedule_interval(self.on_timeout, 1)
def on_timeout(self, *args):
d = datetime.now() - self.start
self.lbl1.text = datetime.utcfromtimestamp(d.total_seconds()).strftime("%H.%M.%S")
class MainApp(App):
def build(self):
c = MyApp()
return c
if __name__ == '__main__':
MainApp().run()
在您的 KV 文件中,id 是 label1,但是当您更新时,您更新的是 lbl1 的文本。