based on the example of this question I want to make a program that shows me a text saved in a variable, "g". Here goes the code:
# config
from kivy.config import Config
Config.set('kivy', 'keyboard_mode', 'system') #¿Para qué sirve esta línea?
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.lang import Builder
from kivy.clock import Clock
from itertools import cycle
import random
Builder.load_file('design.kv')
class MyWidget(BoxLayout):
random_number = StringProperty()
string = StringProperty()
def __init__(self):
super(MyWidget, self).__init__()
self.random_number = str(random.randint(1, 100))+'\n'
self.listText = cycle(('Hola', 'soy', 'una', 'etiqueta'))
Clock.schedule_interval(self.change_label_text, 0.6) #¿Para qué sirve esta línea?
def numero_aleatorio(self):
self.random_number += str(random.randint(1, 100))+'\n'
def change_label_text(self, *args):
self.string = next(self.listText)
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
g = "Esto es un mensaje guardado en una variable" #Aquí está la variable que guarda el texto que quiero mostrar
if __name__ in ('__main__', '__android__'): #¿Para qué sirve esta línea?
myApp().run()
And the file design.kv
:
<MyWidget>:
BoxLayout:
TextInput:
id: textInp2
text: g
multiline: True
readonly: True
background_color: 0.92,0.89,0.75,1
on_focus: self.focus = False
Running it gives me an error: NameError: name 'g' is not defined
How can I make it show me the variable g?
I'm not familiar with kivy, but I think the problem is that you are defining g outside your class, I think it should be something like this:
You must create the variable inside your class
Mywidget
. The variable should be aStringProperty
, if you know Tkinter is similar toStringVar
.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 that it knows how to locate it. In this case the parent isroot
, so it must betext: root.g
The code should be something like this:
main.py:
desing.kv:
I see that you have several comments asking what those lines are, I guess they are covert indirect questions...XD. Let's see if I can clarify something:
Config.set('kivy', 'keyboard_mode', 'system')
: the objectconfig
is used to configure, forgive 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 open in full screen, etc.In this case, what is specified is the type of keyboard that the application must use, it is defined as
'system'
which means that the keyboard that our device has by default, the system keyboard, will be used. If we run the app on the computer it will be the physical keyboard, if it is on Android the keyboard used by the phone by default. There are other possibilities such as the virtual keyboards provided by Kivy, we can even create a customized 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 name indicates, it is a sheduler, it allows executing a given task every x time. In this case what it does is execute the methodself.change_label_text
every 0.6 seconds. If you remember the example of the previous question there was a label that automatically changed the text in a cyclical way, this is what allows it. The funny thing is that unlike methods liketime.sleep()
it doesn't block the interface, that is, the program continues to work and does something every x amount of time.def on_pause(self)
anddef on_resume(self)
: these methods are intended for Android. On Android you can open the app and minimize/pause it. The methodon_pause
prevents minimizing the app from closing it so that we can resume it later. The methodon_resume
specifies the behavior when relaunching it.if __name__ in ('__main__', '__android__'):
: this is not a feature of Kivy, you can see an extensive explanation in an answer I gave back to this question:What is if __name__ == “__main__”:?
The only difference is that thinking that the program runs on Android devices, '__android__' is added. We can use this module by importing it from another, using this construction prevents the GUI from being launched when importing (which is of no interest). Instead, if it is executed as the main module, if the GUI is launched when executing the
run()
.You can change the text in other ways, for example within your class
MyWidget
you could modify the text by accessing the Text edit using its id given in Kivy Language. In this case you don't use theStringProperty
but you modify the text of the Text edit directly. To access any widget through its id, simply access the attributeids
. This attribute is a dictionary, so the id is passed to it as a string as the key (self.ids['id_del_widget']
):main.py:
desing.kv: