I am programming a multi-subsystem project that involves 3 Raspberry Pis chatting to each other over the local network using sockets. On one of the Pis, I am programming a Kivy app.I am trying to get a label in Kivy to change its text when one of the other Pis has verified that it is OK via a socket.
Definition of Label in .kv file:
Label:
id: distance_label
markup:True
text:"[size=40]Distance:[/size]\n\n[size=60][color=ff0000]NO[/color][/size]"
halign:"center"
valign:"top"
color:black
And then there is an entirely separate Python thread that I am manipulating the sockets with. This pings the subsystem, and then tries to change the label on successful reply:
def test_socket(self, connection):
Communication.server.send_data(connection,"VERIFY?")
data_back = Communication.server.receive_data(connection)
print data_back
if data_back == "DISTANCE!":
# set distance to OK
print "Distance is OK"
InitScreen().distance_on()
This then in turn tries to trigger the distance_on() method of the InitScreen class. The method appears to be triggered (and it will print out a small debug message if you put it in there), but annoyingly the label text does not change from its original value.
class InitScreen(Screen):
........
def distance_on(self, *args):
distance_label = self.ids['distance_label']
distance_label.text = "[size=40]Distance:[/size]\n\n[size=60][color=008000]OK[/color][/size]"
print distance_label.text
In another part of the code I use the on_event function in Kivy to change the value of a label using a slider. This works perfectly. I have tried to make my code look as similar as possible to this working instance, the only difference being that I am not trying to trigger from a Kivy event, just within the Python.
Can anyone shed some light? Is it something to do with calling another class' method? An ID issue?
This is not a duplicate question. The suggested duplication involves triggering of functions from Kivy events. I would like to trigger a Kivy label change from Python itself - not a Kivy event like touching a button for example.
Here is all the code in question. I think this issue is conflated by the fact I am using a ScreenManager, but I can't not do that!
PROGRAM:
class Communication(threading.Thread):
server = serv.Server()
def run(self):
self.setup()
while distance == False:
(connection, address) = self.awaiting_socket()
self.test_socket(connection)
def setup(self):
Communication.server.setup_server()
print "SUCCESS ON BIND"
def awaiting_socket(self):
print "AWAITING"
(connection, address) = Communication.server.socket_reception()
return (connection, address)
def test_socket(self, connection):
Communication.server.send_data(connection,"VERIFY?")
data_back = Communication.server.receive_data(connection)
print data_back
if data_back == "DISTANCE!":
# set distance to OK
print "Distance is OK"
application.InitScreen.distance_on()
#global distance
#distance = True
if data_back == "STEPPER!":
# set stepper to OK
print "Stepper is OK"
..............
class InitScreen(Screen):
def power_off(self, *args):
onoffswitch = self.ids["onoffswitch"]
onoff_value = onoffswitch.active
if onoff_value == False:
subprocess.call(powerdown)
def distance_on(self):
distance_label = self.ids["distance_label"]
distance_label.text = "[size=40]Distance:[/size]\n\n[size=60][color=008000]OK[/color][/size]"
class ScreenManagement(ScreenManager):
pass
application = Builder.load_file("main.kv")
class LidarApp(App):
def build(self):
return application
KIVY DEFINING SCREEN MANAGER:
ScreenManagement:
transition: FadeTransition()
InitScreen:
MainScreen:
KIVY DEFINING LABEL IN QUESTION:
Label:
id: distance_label
markup:True
text:"[size=40]Distance:[/size]\n\n[size=60][color=ff0000]NO[/color][/size]"
on_text:
halign:"center"
valign:"top"
color:black