0

I have a big problem . I want to use id of widget like "inputText" in another class kivy. Can anyone know that how can I solve it ?

actually I want to get inputText from User , and create text file and write it , and then in another class ,read path of text file (ti+"/tst.txt",'r') (I cant read ti(id of inputText In My StartTest kv file). and pass it to variables , and then write value of variables in my gui .

problems :

  1. read path of file with my id of inptText-> (ti + ".." )
  2. read text of file and change value of variable -> (wbc) in another class kivy (class test) .

this is my code example :

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager , Screen
from kivy.uix.image import Image
from kivy.core.window import Window
from kivy.base import runTouchApp
from kivymd.app import MDApp
#define our different screen
Window.size = (1024, 768)


class Test(Screen):
    WBC = "-"
      
class ShenacellGif(Screen):
    def hi (self):
        print("hi")
 
    
    def read_data(self):
        infile = open(ti+"/tst.txt",'r')
        print(infile.readline())
        print(infile.read())
        asd=infile.read()
        infile.close()
        

class StartTestBtn(Screen):
    pass


class WindowManager(ScreenManager):
    pass


class MyGridLayout(Widget):
    pass

Builder.load_string("""
#:import utils kivy.utils
<WindowManager>:
    StartTestBtn:
    Test:
    ShenacellGif:
    
    
<RoundedButton@Button>:
    background_color:(0,0,0,0)
    bachground_normal: ""
    canvas.before:
        Color:
            rgba:(21/255.0,33/255.0,55/255.0,1)
        RoundedRectangle:
            size: self.size
            pos: self.pos
            radius: [35]

<ShenacellGif>:
    name : "sh"
    orientation: "vertical"
  
    BoxLayout:
        orientation: 'vertical'
        padding: 100 , 10 , 800 , 650

        RoundedButton:
            size_hint_y: None
            size_hint_x: None
            height: '48dp'
            pos:200,200
            font_size:40
            width: 100
            height: 100
#             on_press: root.read_data()
            on_release: app.root.current = "test"

<StartTestBtn>:
    name: "startTestBtn"
    BoxLayout:
        id: a
        cols:1
        orientation: "vertical"
        size: root.width , root.height
        spacing: 20
        padding: 200, 250, 250 , 200
        TextInput:
            id : ti
            text : ti.text
            multiline:False
            size_hint_x: 1
            height: 50
            size_hint_y: None
            width: 200
            hint_text: "Enter User ID"
            icon_right: "account"
            font_size:24
        Button:
            id : b
            text: "start Test"
            font_size: 28
            size_hint_x: 1
            height:50
            size_hint_y: None
            width:1503
            on_release:
                f = open(ti.text, "w")
                f.write(ti.text)
                f.close() 
            on_release: app.root.current = "sh"
            background_color : (34/255.0,59/255.0,74/255.0,1)

<Test>:
    name: "test"     
    BoxLayout:
        orientation: 'vertical'
        padding: 100 , 10 , 800 , 650 
        
    BoxLayout:
        padding: 200 , 50 , 50 , 50
        spacing: 90
        orientation: "horizontal"
        size_hint: .45,1.55
     #    pos_hint: {'x': 0, 'y': 1}  
        Label:
            text: "Result"
            font_size:20
        Label:
            text: "Unit"
            font_size:20
        Label:
            text: "Refence"
            font_size:20
            
   
            
    BoxLayout:
        padding: 100 , 50 , 50 , 20
        spacing: -10
        orientation: "vertical"
        size_hint: -1,.85
     #    pos_hint: {'x': 0, 'y': 1}  
        Label:
            text: "WBC"
            color: 87,103,102,100
            font_size:20
      
        
    BoxLayout:
        padding:200 , 50 , 50 , 20
        spacing: -10
        orientation: "vertical"
        size_hint: -1,.85
     #    pos_hint: {'x': 0, 'y': 1}  
        Label:
            text: root.WBC
            color: 0,1,1,1
             
    BoxLayout:
        padding: 300 , 50 , 50 , 20
        spacing: -10
        orientation: "vertical"
        size_hint: -1,.85
        Label:
            text: "10^3/ul"
        
    BoxLayout:
        padding: 400 , 50 , 50 , 20
        spacing: -10
        orientation: "vertical"
        size_hint: -1,.85 
        Label:
            text: "4.5 - 11.5"
         
        
              
""")



class MyLayout(Widget):
    pass

class Shenacell(MDApp):
    def build(self):
        self.theme_cls.theme_style = "Dark"
        self.theme_cls.primary_palette = "BlueGray"
        return WindowManager()

if __name__ == '__main__' :
    Shenacell().run()

help me plz :(

siitaw
  • 107
  • 3
  • 12

1 Answers1

0

You can get the TextInput data using ids like this:

class Test(Screen):
    WBC = StringProperty("-")  # make WBC a property

class ShenacellGif(Screen):
    def read_data(self):
        # get the TextInput data
        ti = self.manager.get_screen('startTestBtn').ids.ti.text
        print('ti:', ti)

        # after reading some file, set the WBC property
        self.manager.get_screen('test').WBC = 'Abba'

Using a StringProperty for the WBC variable lets kivy automatically update the Label that references it.

All Screen instances that are children of a ScreenManager have a manager attribute that references the ScreenManager, then, using the get_screen() method you can access a particular Screen, and then you can use the ids for that Screen.

John Anderson
  • 35,991
  • 4
  • 13
  • 36