如何使用kivy从TextInput写入并保存到文本文件中

时间:2014-02-13 19:30:22

标签: android python kivy

我想在TextInput小部件中输入文本以将其保存到文本文件中。请有人向我展示如何获取在TextInput小部件中输入的值以将其保存到文本文件中的示例。

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
import os

此方法尝试保存到文本文件中但不起作用

def save(self,nam):
    fob = open('c:/test.txt','w')
    write =    fob.write(str(name))


Builder.load_string('''
<MenuScreen>:
    BoxLayout:
        Button:
            text: 'Add New Staff'
            on_press: root.manager.current = 'add_staff'
        Button:
            text: 'View Staff Profile'
        Button:
            text: 'Salary report'

<Add_new_staff>:
    nam: str(name_input)
    job: job_input
    GridLayout:
        cols: 2 
        Label:
            text: 'Name'
        TextInput:
            id: name_input
            multiline: False
        Label:
            text: 'Job'
        TextInput:
            id: job_input
        Label:
            text: 'Salary'
        TextInput:
        Label:
            text: 'Date of Joining'
        TextInput:
        Button:
            text: 'Back to menu'
            on_press: root.manager.current = 'menu'
        Button:
            text: 'Save'
            on_press: app.save(self,nam)
''')


class MenuScreen(Screen):
    pass

class Add_new_staff(Screen):
    pass

sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(Add_new_staff(name='add_staff'))

class TestApp(App):
    def build(self):
        return sm



if __name__ == '__main__':
    TestApp().run()

2 个答案:

答案 0 :(得分:2)

这是你工作的例子。

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput


Builder.load_string('''
<MenuScreen>:
    BoxLayout:
        Button:
            text: 'Add New Staff'
            on_press: root.manager.current = 'add_staff'
        Button:
            text: 'View Staff Profile'
        Button:
            text: 'Salary report'

<Add_new_staff>:
    nam: str(name_input)
    job: job_input
    GridLayout:
        cols: 2
        Label:
            text: 'Name'
        TextInput:
            id: name_input
            multiline: False
        Label:
            text: 'Job'
        TextInput:
            id: job_input
        Label:
            text: 'Salary'
        TextInput:
        Label:
            text: 'Date of Joining'
        TextInput:
        Button:
            text: 'Back to menu'
            on_press: root.manager.current = 'menu'
        Button:
            text: 'Save'
            on_press: app.save(name_input.text, job_input.text)
''')


class MenuScreen(Screen):
    pass

class Add_new_staff(Screen):
    pass

sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(Add_new_staff(name='add_staff'))

class TestApp(App):
    def build(self):
        return sm

    def save(self, name, job):
        fob = open('c:/test.txt','w')
        fob.write(name + "\n")
        fob.write(job)
        fob.close()    

if __name__ == '__main__':
    TestApp().run()

但是几句建议。  1.而是使用数据库(sqlite3?)来存储这些数据。它可以更有效地扩展,在数据变大时为您提供更快的查找速度。  2.存储您的数据是所有用户的“读/写”位置。 Kivy为此提供了便利功能。

http://kivy.org/docs/api-kivy.app.html?highlight=data_dir#kivy.app.App.user_data_dir

希望有帮助吗?

干杯

答案 1 :(得分:1)

尝试创建文件夹2 c:\test

并保存到{而不是C:\

class TestApp(App):
    def build(self):
        return sm
    def save(self,nam):
        fob = open('c:/test/test.txt','w')
        write =    fob.write(str(name))

可以防止您提到的错误