globalize variables

23 views
Skip to first unread message

Sadaka Technology

unread,
Dec 28, 2020, 6:44:39 AM12/28/20
to Kivy development
class main(..):

    def init.....:
        super.....
        button = Button(on_release=self.on_click)

    def on_click(self,screen,*args)
          self.clear_widgets()
          if screen == '2nd':
              float = 2nd()
          elif screen == '1st':
              float = 1st()
          self.add_widget(float)

class 2nd(...):
    string_text = StringProperty()
    def init.....:
        super.....
        Text = TextInput(text=self.string_text)
         button=Button(on_release=self.on_click)

    def on_click
          self.parent.on_click('1st')

class 1st(...):
     ............


#my only issue is in class 2nd() how can I change the value of the variable string_text, if I am clearing widgets and adding widget again in parent class ( main()), then string_text will again equal to empty ,   I tried to add string_text variable in main() but the same issue I am getting, how can I save the string_text in 2nd() if am removing it and adding it again ?

John

unread,
Jan 28, 2021, 2:13:39 AM1/28/21
to Kivy development

from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput

from kivy.properties import StringProperty

from kivy.app import App


class Second(RelativeLayout):
    string_text = StringProperty("")

    def __init__(self, *args, **keys):
        super(Second, self).__init__(*args, **keys)
        text_input = TextInput(
            size_hint=(None, None),
            size=(300, 250),
            text=self.string_text,
            pos=(0, 0)
        )

        def update_string_text(*args, **keys):
            self.string_text = text_input.text
        text_input.bind(focus=update_string_text)
        button = Button(
            size_hint=(None, None),
            size=(300, 250),
            text="Click (Second)",
            pos=(0, 300)
        )

        button.bind(on_release=self.on_click)

        self.add_widget(text_input)
        self.add_widget(button)

    def on_click(self, screen, *args, **keys):
        print("In Second")
        self.parent.on_click('1st')


class Main(RelativeLayout):

    saved_string = ""

    _button = None

    def __init__(self, *args, **keys):
        super(Main, self).__init__(*args, **keys)
        self._button = Button(
            size_hint=(None, None),
            size=(100, 100),
            text="Click (Main)",
            pos=(300, 300)
        )
        self._button.bind(on_release=self.on_click)

        second = Second(
            size_hint=(None, None),
            size=(100, 100),
            pos=(0, 0),
            string_text="sample text"
        )

        self.add_widget(self._button)
        self.add_widget(second)

    def on_click(self, screen, *args, **keys):
        # find the widget of type Second
        widget = None
        for wid in self.children:
            # print(type(wid))
            if (type(wid) is Second):
                widget = wid
                break

        self.clear_widgets()

        if (widget != None and hasattr(widget, "string_text") == True):
            self.saved_string = str(widget.string_text)

        # to show you that the string value is preserved
        print(type(self.saved_string), self.saved_string)

        wid = None
        if screen == '2nd':
            wid = Second(
                size_hint=(None, None),
                size=(100, 100)
            )
        elif screen == '1st':
            wid = Second(
                size_hint=(None, None),
                size=(100, 100)
            )
        # print(wid)  # for verification
        if (wid != None):
            self.add_widget(wid)

        self.add_widget(self._button)


class AppTest(App):

    def on_start(self):
        pass

    def on_stop(self):
        pass

    def on_pause(self):
        return True

    def on_resume(self):
        pass

    def build(self):
        # self.icon = ""
        self.title = "AppTest"

        layout = RelativeLayout()

        main = Main()

        layout.add_widget(main)

        return layout


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


Try the above code.  I believe the example contains the solution you are looking for.  Know that you must cast a StringProperty string to a python string utilizing the built-in str() function to obtain the string without a weak reference to the StringProperty element that one obtains from direct assignment.

i.e., when going from StringProperty to str: str(self.string_property_string)...
...then you may save the string to a class variable or wherever is appropriate.

Cheers,

Reply all
Reply to author
Forward
0 new messages