Home > database >  How to pass a variable from json to Kivy? Python3
How to pass a variable from json to Kivy? Python3

Time:11-13

I'm making an application in Python3/Kivy/Json. I can't pass part of the array from Json to Kv via StringProperty. When you click on the Find button, a value from Json should be passed. Empty space no text in the MDTextField: hint_text: line. And in MDLabel: too how can this be done?

copymain.kv

KV=
#:kivy 2.1.0
#:import SlideTransition kivy.uix.screenmanager.SlideTransition
#:import GridLayout kivy.uix.gridlayout.GridLayout
#:import TextInput kivy.uix.textinput
#:import Label kivy.uix.label
#:import MDLabel kivymd.uix.label

ScreenManagement:
    transition: SlideTransition()
    Lce_InputScreen:
        id: lce_input

<Lce_InputScreen>
    name: "lce_input"
    input_code_out: input_code_out
    GridLayout:
        cols: 2
        padding: 10
        spacing: 50
        size_hint_x: .5
        MDLabel:
            text: "Label: "
        MDLabel:
            text: root.vod_text
        MDTextField:
            id: input_code_out
            multiline: False
            max_text_length: 5
            font_size: 30
            #hint_text: "Enter you Code"
            hint_text: root.vod_text
            adaptive_height: True
            pos_hint: {"x": .05, "y": .9}
        MDRaisedButton:
            text: "Find"
            font_size: "18sp"
            on_release:
                root.check_data_login()
                app.root.current = "lce_input"

main.py


from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import StringProperty, ObjectProperty
import json


class Lce_InputScreen(Screen):
    input_code_out = ObjectProperty(None)
    vod_text = StringProperty('')

    def check_data_login(self):
        test = self.input_code_out.text

        file_name = 'test.json'
        with open(file_name, 'r', encoding='utf-8') as f:
            data = json.loads(f.read())

        taxi = data[0]["prich_error"]
        vod_text = StringProperty(taxi)

        print("texa", type(taxi), taxi)
        print(type(vod_text), vod_text)

class ScreenManagement(ScreenManager):
    pass

class CustomMenuApp(MDApp):
    def build(self):
        return Builder.load_file("copymain.kv")

if __name__ == "__main__":
    CustomMenuApp().run()

test.json

[{
        "id_error": "1", 
        "opis_error": "opis1",
        "prich_error": "prich1"
            },{
        "id_error": "5", 
        "opis_error": "opis5", 
        "prich_error": "opis5"
        }]

CodePudding user response:

You can just assign/pass the required value to the attribute you already defined as follows,

    def check_data_login(self):
        test = self.input_code_out.text

        file_name = 'test.json'
        with open(file_name, 'r', encoding='utf-8') as f:
            data = json.loads(f.read())

        taxi = data[0]["prich_error"]
        self.vod_text = taxi
        print("texa", type(taxi), taxi)
        print(type(self.vod_text), self.vod_text)

  • Related