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

标签 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()

最佳答案

这是您的示例。

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

希望对您有所帮助?

干杯

关于android - 如何使用kivy从TextInput写入并保存到文本文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21763772/

相关文章:

java - Mobile Vision API 读取的条码不正确

java - (按钮)是什么意思?

java - 由于 java.lang.RuntimeException : android. os.TransactionTooLargeException 导致库崩溃:数据包大小 539544 字节

python - 何时使用 new.instancemethod 与将方法分配给类

python - 如何在 Ubuntu 中将 kivy 和 python 打包为可执行文件?

Android 应用程序移动到 sdcard 选项被禁用

python - 从 Pandas Series 中选择行,其中行是数组

python - 使用 pyparsing 匹配非空行

android - Kivy 和 Google Play 服务

python - 如何将Python的日志输出重定向到Kivy标签?