我希望有人能够帮助我完成我的Kivy / Python脚本的一部分。我相信有经验的Kivy用户可以快速找到解决方案。我已经查看了其他帖子(Kivy Update Dynamic Label Text),但对我没有用,我在想,因为它们使用的是旧版本的Kivy(v1.7,我使用的是v1.10)。
我可以在代码中看到my_fun的定期调度函数回调。 for循环的细节是无关紧要的,但在my_fun函数中,ins.a接受my_Array的第三个索引值,调用' on_a'更新值时正常运行。现在,我需要标签' labelA1var'要使用此值更新的文本(而不是x1,目前只是作为占位符)
提前谢谢!
import kivy
kivy.require('1.10.0')
from kivy.app import App
from kivy.uix.button import Label
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Color
from kivy.graphics import vertex_instructions
from kivy.graphics.vertex_instructions import RoundedRectangle, Line
from kivy.properties import NumericProperty, StringProperty
from kivy.config import Config
from kivy.clock import Clock
from kivy.event import EventDispatcher
Config.exit_on_escape = 1
class MainClass(FloatLayout):
layout = FloatLayout(size=Window.size,pos=Window.center)
x1 = 100 #Position all widgets using values
y1 = 100
#Define Labels (left just one label for my example)
labelA1var = Label(text='%d V' %(x1),color = (0,0,0,1),font_size = 20, pos = (-250 +x1, -26 +y1))
#Add Labels to Screen
layout.add_widget(labelA1var)
Window.add_widget(layout)
def my_fun(dt):
x = 0
global my_Array
my_Array = []
print("My Function Running")
read = [0x00, 0x00, 0x42, 0x55, 0x00, 0x00, 0x42, 0x55, 0x00, 0x00, 0x42, 0x55, 0x00, 0x00, 0x42, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
reading = read + read + read + read #256 bytes read ==> 64 32 bit values
for i in range(0,64):
data32 = (reading[x] << 24) | (reading[x+1] << 16) | (reading[x+2] << 8) | reading[x+3]
my_Array.append(data32)
#print('The current uint32 value is: %d' % data32)
x += 4
if x >= 252:
x = 0
print('x reset to zero')
print('my_Array Length is: %s' %len(my_Array))
ins.a = my_Array[2]
print(ins.a)
return my_Array
#Scheduled Fucntion Callbacks
v = 1
#Call on my_fun with frequency v
my_event = Clock.schedule_interval(my_fun,v)
class Events(EventDispatcher):
a = NumericProperty(0)
def on_a(self, instance, value):
print('a updated')
ins = Events()
class KivyApp(App):
def build(self):
self.title = "Change Title" #Set Window Title
Window.fullscreen = 'auto'
Window.clearcolor = (0.25,0.25,0.25,0.7)
return MainClass()
def on_stop(self):
print('Window Closed')
window = KivyApp()
if __name__ == '__main__':
window.run()