I want to load a text from a file in a label. My code in python is:
# 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.lang import Builder
Builder.load_file('design.kv')
class MyWidget(BoxLayout):
def __init__(self):
super(MyWidget, self).__init__()
def showtext(self):
with open("Prueba.txt","r") as f:
filetext = f.read()
#self.ids['label1'].text = filetext
class myApp(App):
def build(self):
return MyWidget()
def on_pause(self):
return True
def on_resume(self):
pass
if __name__ in ('__main__', '__android__'):
myApp().run()
The design.kv file is:
<MyWidget>:
BoxLayout:
Label:
id: label1
text: root.showtext()
I get an error: ValueError: None is not allowed for Label.text
It seems that since I have defined the function showtext
as def showtext(self)
I have to put something where "self" goes in the file design.kv, then I put "label1" keeping the file design.kv like this:
<MyWidget>:
BoxLayout:
Label:
id: label1
text: root.showtext(label1)
Then he gives me the following error: TypeError: showtext () takes 1 positional argument but 2 were given
I do not understand, he says that I have given him two arguments, when I have only given one, label1. Could someone tell me what to put or change to load text from a txt to label?
Thank you very much in advance.