如何使用Kivy从TextInput中编写并保存到文本文件

5
我希望能将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()

当我运行程序并从Add_new_staff添加细节,然后按保存按钮时,它会显示“TestApp”对象没有属性“save”。实际上,我是kivy的新手。 - Nabajyoti Das
请举个例子,说明如何获取“TextInput”小部件中输入的值并将其保存到文本文件中。 - Nabajyoti Das
2个回答

4

这是您的示例程序运行结果。

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

希望能帮到你?干杯

@ZenCODE 你好!我遇到了这个错误:IOError: [Errno 13] Permission denied: 'c:/test.txt' - Abhishek Bhatia

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))

将防止您提到的错误


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接