python – Value of variable sharing, returned by a function, between classes or widgets of Kivy

Evenings or evenings For several days, I racked my brains in search of a way to solve this problem. I have seen Kivy's documentation, video tutorials and blogs with examples, but I can not find a way to solve the problem.

The problem is that I have two widgets:
– One with buttons to perform a series of functions
– Other where to display information

The widget that displays the buttons performs only a small scan of IP addresses connected to one network, another to start an MQTT server and another to shut down the server. => [Buscar dispositivos — Conectar — Desconectar]

In the widget in which the information is displayed, the idea is that at the beginning, when you run the code, it is empty, but when you click the button that searches the available IPs, all the Available IPs will appear in the empty space. button for each IP. Better explain me with a small example:

Empty the information widget >> Press the "Search for devices" button >> Analyze and get the available IP addresses >> Get the IP address – – >> In the information widget, you will find 3 buttons, one for each IP address.

Then on each button with its IP address, when you press the login button, you have to edit the text with the other information that would be returned to each device connected to the MQTT server, and then pressing each button would change the screen and would display more information, etc. things, but that's another problem.

So, how can I make sure that by calling the function searched by IP, from the login button of a widget, I return the information to another widget?

I leave the code that I have up to here both .Py and .KV.


import the package kivy # general to create the interface

since import App # Functions to implement our window or application
from kivy.uix.boxlayout import BoxLayout # Functions to implement a base layer on which elements will be placed
from kivy.uix.anchorlayout import AnchorLayout # Functions to place items in specific places in the presentation
from kivy.uix.gridlayout import GridLayout # Functions for organizing elements in tables
from kivy.uix.button import Button # Functions to manage buttons
from kivy.uix.listview import ListItemButton # Functions to manage lists
from kivy.clock Import Clock # Needed to update items
from kivy.event import EventDispatcher # Needed to create properties and events
from import ListProperty # Import properties from the Kivy list
since kivy.config import Config # for the necessary configurations

import os # Package required for features requiring operating system resources
to Import the Subprocess # Package Needed to Create and Call System Threads

# Screen size settings
Config.set ('graphics', 'width', 1024)
Config.set ('graphics', 'height', 600)
# The buttons, widgets, layout and others are all widgets

list_ip = []   # Empty variable

class functions ():
def ping_scan (self, * arg): # Function to detect devices on the network. With arg, the arguments are introduced
# which will not be taken into account later
ip_list = []
        with open (os.devnull, "wb") as limbo: # devnull is like a bottomless pit from which nothing can be recovered
# and eliminate the error per screen when running
for n in the range (0, 10 + 1): # The + 1 is added to reach the maximum limit entered
ip = "192.168.1. {0}". format (n)
res = subprocess.Popen (['ping', '-n', '1', '-w', '200', ip], stdout = limbo, stderr = limbo) .wait ()
if res:
print ("INACTIVE =>" + ip)
if not:
print ("ACTIVA =>" + ip)
ip_list.append (ip)
return ip_list

class Container (BoxLayout): # We create a Container class that inherits BoxLayout functions
def __init __ (auto):
super (Container, self) .__ init __ () # With super, all properties of the container are inherited. C & # 39;
# needed to add new properties

self.BB = button_box () # Box instance for buttons
self.IB = info_box () # Instanciación a caja for more information
self.LB = logo_box () # Instanciación a caja para logo

self.add_widget (self.BB) # Add the box to the layout
self.BB.add_widget (self.LB) # Add the logo box in the button area
self.add_widget (self.IB) # Add Info Box

class button_box (BoxLayout): # LayOut to add buttons

def __init __ (auto):
# nonlocal list_ip # We make this nonlocal value
super (button_box, self) .__ init __ ()
self.buscar = Button (text = "Search for devices") # Create a button to search for devices
self.con = Button (text = "Connect") # Creates a button to connect to devices
self.desc = Button (text = "Disconnect") # Create a button to disconnect devices
self.add_widget (self.buscar) # Add the search button to LayOut
self.add_widget (self.con) # Add the button to connect to LayOut
self.add_widget (self.desc) # Add logout button to LayOut
list_ip = self.buscar.bind (on_press = functions.ping_scan) # on_press adds arguments to the function. You need to add * arg to
# function. With the on_press function, we detect if a button has been pressed and act if the
# condition

class info_box (BoxLayout): # LayOut to add device information

def __init __ (auto):
super (info_box, auto) .__ init __ ()
#lista_ip = ['0', '1', '2', '3']
        if len (list_ip)! = 0:
for i in the range (len (list_ip)):
self.add_widget (Button (text = ip_list[i]))
if not:
print ("No IP")

class logo_box (AnchorLayout): # LayOut to add the program logo in a corner

class interfazApp (App): # Creation of the application as such. It must have the same name as the .kv file
title = & # 39; Control Center & # 39; # Name of the program

def build (self): # Function to start our application
return container ()

if __name__ == "__main__": # Required, not necessary, for Android and Kivy, it is a convention.
interfaceApp (). run ()

I think it goes without saying that the ping_ip function is responsible for pinging all available IP addresses on the network. Whoever does it between 0 and 10 is simply for simplicity when doing the tests, the range and I will modify it in the final version.


orientation: verticale vertical & # 39;
spacing: 10
# spacing is the space between the widgets
upholstery: 10
# padding is the space between the edge of the window and the content => iz - a - de - ab => List for different
# The instructions on the canvas are graphical instructions for customizing widgets.
rgb: 0, 0, 0
# These are values ​​in both for one. With rgba we add the alpha
size: self.size
pos: self.pos
# self refers to the widget or maximum layout next to the indentation
# In this case, same size and same position as Container

# By default, BoxLayouts are oriented horizontally
spacing: 10
upholstery: 10
size_hint: 1, none
# Disable relative size in X and Y
# width: 650
height: 50
#rgb: 0.78, 0.78, 0.78
rgb: 0.65, 0.65, 0.65
size: self.size
pos: self.pos

orientation: verticale vertical & # 39;
spacing: 10
upholstery: 10
rgba: 1, 1, 1, 0.25
size: self.size
pos: self.pos
# Label:
# markup: True
# text: "[ref=prueba]hi world[/ref]"
# # In order for the item in the list to have an invisible button, you must add [ref=referencia]Text[/ref]
# # With on_ref_press, we do this by clicking on the reference created in the previous line of code, something is executed
# on_ref_press: print ("Hello holita")

spacing: 2
upholstery: 5
size_hint: None, None
width: 40
height: 32
source: & # 39; UNIT_n.png & # 39;
size: self.size
pos: self.pos

I imagine that all I want to do, is with properties and it's easier than it looks, but I do not know how to do it. I would also like, if possible, to tell me if there is an error or something that could be done more simply.

I think nothing escapes to indicate, but to miss something, let me know and I'll give you the information that might be missing.

I thank you in advance.