[英]How to update a label in Kivy every second
你好开发者,来自奥地利的问候
目前,我正在尝试弄清楚如何永久更新 Kivy 界面中的标签。
情况:对于我的毕业论文,我必须为驾驶机器人开发 GUI。 在这个用户界面中,有一个标签应该显示机器人的当前速度。 速度由 Raspberry PI 测量并使用套接字发送到用户界面
我已经设法更改了标签,但前提是界面中的操纵杆被移动了。
def _update_joystick_output(self, instance, pad): #send joystick data
global driving_assistant
global connection_check_rpi
global joystick_data
global m
m = str(instance.magnitude)[0:5]
a = str(instance.angle)[0:5]
joystick_data=m+" "+a
self.root.update_label(m) #This command updates the label in the Interface
if connection_check_rpi == True:
if(len(joystick_data) <= 12):
s1.send(str.encode(joystick_data))
我已经尝试编写一个线程,它以与上面相同的方式每半秒自动更新一次标签,但它在任何情况下都不起作用。
class Velocity(threading.Thread):
def run(self):
global vel
global s1
global connection_check_rpi
self.root = Interface()
while True:
if connection_check_rpi == True:
try:
rpm_data = s1.recv(1024)
vel = rpm_data.decode('utf-8')
self.root.update_label
except:
break
else:
break
这是完整的代码(不要介意缩进)
import threading
import time
import socket
from kivy.config import Config
from kivy.app import App
from kivy.garden.joystick import Joystick
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.uix.label import Label
from kivy.properties import StringProperty
from kivy.core.window import Window
Window.clearcolor = (1, 1, 1, 1)
global connection_check_rpi
global connection_check_cam
global driving_assistant
global vel
global m
global cruise_control
global tempHelper
global test_check
vel = 0
tempHelper = 0
driving_assistant=False
cruise_control = False
test_check=False
connection_check_rpi=False
connection_check_cam=False
presentation = Builder.load_string( #place all objects in GUI
"""
#File name: accept_SSCc.py
#: import Joystick kivy.garden.joystick.Joystick
<Interface>:
Image:
id: pic
source:'c:/python/bg.png'
pos: root.width/2-60, root.height/2-50
Label:
id: con1
text: '[color=010203]'+root.connection1+'[/color]'
pos: root.width/2-60, -root.height/2+40
markup: True
Label:
id: con2
text: '[color=010203]'+root.connection2+'[/color]'
pos: root.width/2-67, -root.height/2+15
markup: True
Label:
id: auto
text: '[size=20][color='+root.colorauto+']'+root.auto_label+'[/color][/size]'
pos: 0, -root.height/2+70
markup: True
Label:
id: rpm
text: '[size=40][color=0039FF]'+root.rpm+'[/color][/size]'
pos: 0, -root.height/2+120
markup: True
Label:
id: cc
text: '[size=20][color='+root.colorcc+']'+root.cc_label+'[/color][/size]'
pos: 0, -root.height/2+50
markup: True
Label:
id: value
text: '[size=20][color=000000]'+root.cc_value+'[/color][/size]'
pos: root.width/2-170, -root.height/2+200
markup: True
Button:
text: "ANBS"
font_size: 40
size_hint: None, None
size: 175, 120
color: (1, 1, 1, 1)
background_color:(0, 0, 0, 1)
pos: root.width/2-320, 2*root.height/7-110
on_press: root.button_auto()
Button:
text: "CC"
font_size: 40
size_hint: None, None
size: 180, 60
color: (1, 1, 1, 1)
background_color:(0, 0, 0, 1)
pos: root.width/2+140, 2*root.height/7-50
on_press: root.button_cc()
Button:
text: "+"
font_size: 40
size_hint: None, None
size: 90, 60
color: (1, 1, 1, 1)
background_color:(0, 0, 0, 1)
pos: root.width/2+140, 2*root.height/7-110
on_press: root.button_faster()
Button:
text: "-"
font_size: 40
size_hint: None, None
size: 90, 60
color: (1, 1, 1, 1)
background_color:(0, 0, 0, 1)
pos: root.width/2+230, 2*root.height/7-110
on_press: root.button_slower()
Button:
text: "sh"
font_size: 40
size_hint: None, None
size: 200, 70
color: (1, 1, 1, 1)
background_color:(0, 0, 0, 1)
pos: root.width/2+120, 1.7*root.height/2-10
on_press: root.button_sh()
Button:
text: "dc"
font_size: 40
size_hint: None, None
size: 200, 70
color: (1, 1, 1, 1)
background_color:(0, 0, 0, 1)
pos: root.width/2-100, 1.7*root.height/2-10
on_press: root.button_dc()
Button:
text: "con"
font_size: 40
size_hint: None, None
size: 200, 70
color: (1, 1, 1, 1)
background_color:(0, 0, 0, 1)
pos: root.width/2-320, 1.7*root.height/2-10
on_press: root.button_con()
Joystick:
outer_size: 0.5
inner_size: 0.25
pad_size: 0.5
outer_line_width: 0.01
inner_line_width: 0.01
pad_line_width: 0.01
outer_background_color: (0.5, 0, 0, 1)
outer_line_color: (0, 0, 0, 1)
inner_background_color: (0.2, 0, 0, 1)
inner_line_color: (0, 0, 0, 1)
pad_background_color: (0, 0, 0, 0.96)
pad_line_color: (1, 0, 0, 0.5)
size_hint: 0.5, 0.5
pos: root.width/4, root.height/4+20
""")
class Interface(FloatLayout):
connection1=StringProperty()
connection2=StringProperty()
auto_label=StringProperty()
cc_label = StringProperty()
rpm = StringProperty()
colorcc = StringProperty()
colorauto = StringProperty()
cc_value = StringProperty()
def __init__(self, **kwargs):
global vel
super(Interface, self).__init__(**kwargs)
self.connection1 = " rpi: dc"
self.connection2 = " cam: dc"
self.auto_label = "Notbremssystem off"
self.cc_label = "CruiseControl off"
self.stop_platzhalter = ""
self.rpm = str(vel)+" km/h"
self.colorauto = "FF0000"
self.colorcc = "FF0000"
self.cc_value = "Control: None"
def button_test(self, *args):
global connection_check_rpi
global connection_check_cam
global command
global test_check
global s1
global joystick_data
if connection_check_rpi == True:
if test_check==False:
test_check = True
command="test_on"
s1.send(str.encode(command))
joystick_data = "1.0 90"
print(joystick_data)
s1.send(str.encode(joystick_data))
elif test_check==True:
test_check=False
command="test_off"
s1.send(str.encode(command))
joystick_data = "0.0 90"
print(joystick_data)
s1.send(str.encode(joystick_data))
def button_cc(self, *args):
global connection_check_rpi
global connection_check_cam
global command
global cruise_control
global s1
global joystick_data
global tempHelper
if connection_check_rpi == True: #gehört noch auf true geändert
if cruise_control == False:
cruise_control = True
command="cc_on"
s1.send(str.encode(command))
tempHelper = 6
print(tempHelper)
self.cc_label = "CruiseControl on"
self.colorcc = "009910"
self.cc_value = "Control: %i km/h" % tempHelper
elif cruise_control == True:
cruise_control = False
command="cc_off"
s1.send(str.encode(command))
tempHelper = 0
print(tempHelper)
self.cc_label = "CruiseControl off"
self.colorcc = "FF0000"
self.cc_value = "Control: None"
def button_faster(self, *args):
global connection_check_rpi
global connection_check_cam
global cruise_control
global command
global s1
global joystick_data
global tempHelper
if connection_check_rpi == True and cruise_control == True: #auf True
if tempHelper < 10.5:
tempHelper += 1
print(tempHelper)
command="cc_faster"
s1.send(str.encode(command))
self.cc_value = "Control: %i km/h" % tempHelper
def button_slower(self, *args):
global connection_check_rpi
global connection_check_cam
global cruise_control
global command
global s1
global joystick_data
global tempHelper
if connection_check_rpi == True and cruise_control == True: #auf True
if tempHelper > 4.5:
tempHelper -= 1
print(tempHelper)
command="cc_slower"
s1.send(str.encode(command))
self.cc_value = "Control: %i km/h" % tempHelper
def button_sh(*args): #shut servers down
global connection_check_rpi
global connection_check_cam
global s1
global s2
command="sh"
if connection_check_rpi == True:
s1.send(str.encode(command))
s1.close()
if connection_check_cam == True:
s2.send(str.encode(command))
s2.close()
Smart3DControl().stop()
def button_dc(self, *args): #disconnect from server
global connection_check_rpi
global connection_check_cam
global s1
global s2
command="dc"
if connection_check_rpi == True:
s1.send(str.encode(command))
s1.close()
connection_check_rpi=False
if connection_check_cam == True:
s2.send(str.encode(command))
s2.close()
connection_check_cam=False
self.connection1 = " rpi: dc"
self.connection2 = " cam: dc"
print("disconnected")
def button_auto(self, *args): #de/activate driving assistant
global s1
global s2
print("auto")
command="auto"
if connection_check_rpi == True and connection_check_cam == True:
s1.send(str.encode(command))
s2.send(str.encode(command))
global driving_assistant
if driving_assistant == False:
driving_assistant = True
self.auto_label = "Fahrassistent on"
self.colorauto = "009910"
elif driving_assistant == True:
self.auto_label = "Fahrassistent off"
self.colorauto = "FF0000"
driving_assistant = False
def button_con(self, *args): #connect to servers
global connection_check_rpi
global connection_check_cam
global s1
global s2
print("con")
host1 = '192.168.42.1' # raspberry
port1 = 5560
host2 = '192.168.42.2' # camera
port2 = 5568
try:
if connection_check_rpi == False:
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s1.connect((host1, port1))
print(str(s1))
self.connection1=str("rpi: "+host1)
print("connected to rpi")
connection_check_rpi=True
print(s1)
thread = Velocity()
thread.start()
thread1 = Ausgabe()
thread1.start()
if connection_check_cam == False:
s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2.connect((host2, port2))
self.connection2=str("cam: "+host2)
print("connected to camera")
connection_check_cam=True
print(s2)
except:
pass
def update_label(self, m, *args):
global vel
global cruise_control
global s1
global test_check
self.cc_label = "CruiseControl off"
self.colorcc = "FF0000"
self.cc_value = "Control: None"
if cruise_control == True:
command="cc_off"
s1.send(str.encode(command))
cruise_control = False
if test_check == True:
command="test_off"
s1.send(str.encode(command))
test_check = False
if str(vel)[1:2] is ".":
vel = str(vel)[:4]
else:
vel = str(vel)[:5]
#print(m)
if m == 0:
self.rpm = "0 km/h"
else:
self.rpm = str(vel)+" km/h"
#print(self.rpm)
class Smart3DControl(App):
global vel
global m
def build(self):
self.root = Interface()
joysticks = self.get_joystick(self.root) #assign joystick to variable
for joystick in joysticks:
joystick.bind(pad=self._update_joystick_output) #bind function to joystick
def get_joystick(self, parent):
joysticks = []
if isinstance(parent, Joystick):
joysticks.append(parent)
elif hasattr(parent, 'children'):
for child in parent.children:
joysticks.extend(self.get_joystick(child))
return joysticks
def _update_joystick_output(self, instance, pad): #send joystick data
global driving_assistant
global connection_check_rpi
global joystick_data
global m
m = str(instance.magnitude)[0:5]
a = str(instance.angle)[0:5]
joystick_data=m+" "+a
#print(joystick_data)
self.root.update_label(m)
if connection_check_rpi == True:
if(len(joystick_data) <= 12):
s1.send(str.encode(joystick_data))
class Velocity(threading.Thread):
def run(self):
global vel
global s1
global connection_check_rpi
self.root = Interface()
while True:
if connection_check_rpi == True:
try:
rpm_data = s1.recv(1024)
vel = rpm_data.decode('utf-8')
self.root.update_label
except:
break
else:
break
Smart3DControl().run()
一种方法是使用Clock
每隔例如 250 毫秒调用一次回调。
将此添加到您的build
方法中:
def build(self):
self.root = Interface()
joysticks = self.get_joystick(self.root) #assign joystick to variable
for joystick in joysticks:
joystick.bind(pad=self._update_joystick_output) #bind function to joystick
from functools import partial
from kivy.clock import Clock
Clock.schedule(partial(self.root.update_label, m), .25)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.