You must create the variable within your class Mywidget
. The variable should be a StringProperty
, if you know Tkinter is similar to StringVar
.
Once defined here you can modify it within the class whenever you want. In desing.kv
you must refer to the parent of the variable so you can locate it. In this case the father is root
, so it should be text: root.g
The code should look something like this:
main.py:
# config
from kivy.config import Config
Config.set('kivy', 'keyboard_mode', 'system')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.lang import Builder
Builder.load_file('design.kv')
class MyWidget(BoxLayout):
g = StringProperty()
def __init__(self):
super(MyWidget, self).__init__()
self.g = "Esto es un mensaje guardado en una variable"
class myApp(App):
def build(self):
return MyWidget()
def on_pause(self):
return True
def on_resume(self): #¿Para que sirve esta función?
pass
if __name__ in ('__main__', '__android__'):
myApp().run()
desing.kv:
<MyWidget>:
TextInput:
id: textInp2
text: root.g
multiline: True
readonly: True
background_color: 0.92,0.89,0.75,1
on_focus: self.focus = False
I see you have several comments asking what those lines are, I guess they are indirect covert questions ... XD. Let's see if I can clarify something:
-
Config.set('kivy', 'keyboard_mode', 'system')
: the object config
serves to configure, worth the redundancy, internal aspects of the kivy itself. For example, it is used to indicate the maximum frames per second that the app should have, window icon, if it should be opened in full screen, etc.
In this case what is specified is the type of keyboard that the application should use, it is defined as 'system'
which means that the keyboard that our device has by default, that of the system will be used. If we run the app on the computer, it will be the physical keyboard, if Android is the keyboard that uses the phone by default. There are other possibilities such as virtual keyboards that provide kivy, we can even create a custom virtual keyboard ourselves defined in a json.
You can see the full options in the kivy.config documentation .
-
Clock.schedule_interval(self.change_label_text, 0.6)
: as its own name indicates is a sheduler, it allows to execute a given task every x time. In this case, what you do is to use the self.change_label_text
method every 0.6 seconds. If you remember the example of the previous question there was a label that changed the text automatically in a cyclical way, this is what allows it. The funny thing is that unlike methods like time.sleep()
does not block the interface, that is, the program keeps working and every time it does something.
-
def on_pause(self)
and def on_resume(self)
: these methods are intended for Android. In Android you can open the app and minimize / pause it. The on_pause
method avoids that when you minimize the app it is closed so that we can resume it later. The on_resume
method specifies the behavior when you re-launch it.
-
if __name__ in ('__main__', '__android__'):
: this is not a characteristic of Kivy, you can see an extensive explanation in an answer that I gave in your day to this question:
What is if __name__ == "__main __":?
The only difference is that thinking that the program runs on Android devices adds '__android__'. We can use this module by importing it from another, using this construction prevents the GUI from being launched when importing (which is not interesting). On the other hand, if it is executed as the main module if the GUI is launched when executing the run()
method.
You can change the text in other ways, for example within your class MyWidget
you could modify the text by accessing the Text edit by its id given in Kivy Languaje. In this case you do not use the StringProperty
but you modify the text of the text edit directly. To access any widget using its id, simply access the ids
attribute. This attribute is a dictionary, so the id in the form of a string is passed as a key ( self.ids['id_del_widget']
):
main.py:
# config
from kivy.config import Config
Config.set('kivy', 'keyboard_mode', 'system')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.lang import Builder
Builder.load_file('design.kv')
class MyWidget(BoxLayout):
g = StringProperty()
def __init__(self):
super(MyWidget, self).__init__()
g = "Esto es un mensaje guardado en una variable"
self.ids['textInp2'].text = g
class myApp(App):
def build(self):
return MyWidget()
def on_pause(self): #¿Para qué sirve esta función?
return True
def on_resume(self): #¿Para que sirve esta función?
pass
if __name__ in ('__main__', '__android__'): #¿Para qué sirve esta línea?
myApp().run()
desing.kv:
<MyWidget>:
TextInput:
id: textInp2
multiline: True
readonly: True
background_color: 0.92,0.89,0.75,1
on_focus: self.focus = False