Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write and save into a text file from TextInput using kivy

I would like to get text entered in the TextInput widget to save it into a textfile. Please somebody show me an example how to get values entered in TextInput widget to save it into a text file.

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

This method was a try to save into a text file but it didn't work

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()
like image 739
Nabajyoti Das Avatar asked Feb 13 '14 19:02

Nabajyoti Das


1 Answers

Here is your example working.

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

But a few words of advice. 1. Rather use a database (sqlite3?) to store such data. It will scale more effectively, giving you much faster lookups when your data gets larger. 2. Store your data is "read/write" location for all users. Kivy offers a convenience function for this.

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

Hope that helps?

Cheers

like image 89
ZenCODE Avatar answered Oct 11 '22 18:10

ZenCODE