问题
So I was trying to create a very basic text editor in Kivy. So I had got around the issue of text not showing up (via lambda). However, a new error appeared on the horizon; saving. I want to save automatically to a plain txt file. However, my current code only save an object (is it a pointer?) of the actual text input. Thank you all, SO!
import kivy
import os
kivy.require('1.10.1') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.lang import Builder
class ColdKivyApp(App): # I actually used to call it Zone but changed it to Cold cause it's cold outside ;)
def build(self):
f = BoxLayout(orientation='vertical')
txt = TextInput(multiline=True, cursor_blink=True, background_color=(1,1,1,1))
f.add_widget(txt)
txtstr = str(txt)
Clock.schedule_once(lambda *args: setattr(txt, "focus", True))
with open('testtxt.txt', 'w') as txtwriter:
txtwriter.write("" + txtstr)
txtwriter.close()
return f
if __name__ == '__main__':
ColdKivyApp().run()
Edit: Spelling
回答1:
TextInput has a text property, that's where the current content is.
回答2:
Replace txtstr = str(txt) in your code to txtstr = txt.text.
来源:https://stackoverflow.com/questions/54900269/kivy-does-not-output-plain-text-only-object-location