简体   繁体   中英

Kivy - Passing variables on app startup from a file

I have a text file I am using as a calibration file for a rudder indicator I am making. (It stores the port and stbd limits and the center position) I would like to call this file when the program is booted so it had the same calibration settings from previously.

I can store the 3 numbers as a str in a .txt file and know how to recall them as a list. My thought is to run a function when the app starts defining each part of the list as a variable eg.

calibrationfile1 = open('calfile.txt','r')
lines = calibrationfile1.readlines()
calvalue1 = lines[0].replace(",","").replace("[","").replace("]","")
calvalue = calvalue1.split()

rudderlimits = calvalue 

port_rudder_limit = rudderlimits[0]
stbd_rudder_limit = rudderlimits[1]
center_position = rudderlimits[2]

how do I do call this in a function at startup and make the variables available in another function I dont want to use 'global'?

I have already made a funciton that is a calibration that creates this calfile.txt and it works.

thanks for your help :)

you can load the file on the app on_start method

class YourApp(App):
    def on_start(self):
          self.calibration_data = your_file_loading_function() # returns a dict?

... # other places in your code
class Popcorn(Widget):
    def on_callback(self):
        port_rudder_limit = App.get_running_app().calibration_data['port_rudder_limit']
        ... # do something....

or from kv file

<MyWidget>:
     port_rudder_limit: app.calibration_data['port_rudder_limit']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM