Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

kivy FileChooser

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.factory import Factory
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup

import os


class LoadDialog(FloatLayout):
    load = ObjectProperty(None)
    cancel = ObjectProperty(None)


class SaveDialog(FloatLayout):
    save = ObjectProperty(None)
    text_input = ObjectProperty(None)
    cancel = ObjectProperty(None)


class Root(FloatLayout):
    loadfile = ObjectProperty(None)
    savefile = ObjectProperty(None)
    text_input = ObjectProperty(None)

    def dismiss_popup(self):
        self._popup.dismiss()

    def show_load(self):
        content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
        self._popup = Popup(title="Load file", content=content,
                            size_hint=(0.9, 0.9))
        self._popup.open()

    def show_save(self):
        content = SaveDialog(save=self.save, cancel=self.dismiss_popup)
        self._popup = Popup(title="Save file", content=content,
                            size_hint=(0.9, 0.9))
        self._popup.open()

    def load(self, path, filename):
        with open(os.path.join(path, filename[0])) as stream:
            self.text_input.text = stream.read()

        self.dismiss_popup()

    def save(self, path, filename):
        with open(os.path.join(path, filename), 'w') as stream:
            stream.write(self.text_input.text)

        self.dismiss_popup()


class Editor(App):
    pass


Factory.register('Root', cls=Root)
Factory.register('LoadDialog', cls=LoadDialog)
Factory.register('SaveDialog', cls=SaveDialog)


if __name__ == '__main__':
    Editor().run()
Comment

kivy FileChooser kv

#:kivy 1.1.0

Root:
    text_input: text_input

    BoxLayout:
        orientation: 'vertical'
        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: 'Load'
                on_release: root.show_load()
            Button:
                text: 'Save'
                on_release: root.show_save()

        BoxLayout:
            TextInput:
                id: text_input
                text: ''

            RstDocument:
                text: text_input.text
                show_errors: True

<LoadDialog>:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser

        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: "Cancel"
                on_release: root.cancel()

            Button:
                text: "Load"
                on_release: root.load(filechooser.path, filechooser.selection)

<SaveDialog>:
    text_input: text_input
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser
            on_selection: text_input.text = self.selection and self.selection[0] or ''

        TextInput:
            id: text_input
            size_hint_y: None
            height: 30
            multiline: False

        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: "Cancel"
                on_release: root.cancel()

            Button:
                text: "Save"
                on_release: root.save(filechooser.path, text_input.text)
Comment

PREVIOUS NEXT
Code Example
Python :: excel write column 
Python :: proper function pandas 
Python :: pickled list 
Python :: chi square test contingency table python 
Python :: python sort 
Python :: get reactions from message discord.py 
Python :: how to find and remove certain characters from text string in python 
Python :: create gui python 
Python :: python number of lines in file 
Python :: flask on gevent over https 
Python :: NLP text summarization with Luhn 
Python :: how to count substring in a string in python 
Python :: python set attribute by string name 
Python :: decision tree classifier example 
Python :: #pip install commands 
Python :: #remove a sublist from a list-use remove method 
Python :: list all files in python 
Python :: split by backslash python 
Python :: python check None 
Python :: upload bytes to s3 python 
Python :: get first letter of each word in string python 
Python :: select rows with multiple conditions pandas query 
Python :: convert python project to exe 
Python :: python list as queue 
Python :: how to change the name of a variable in a loop python 
Python :: Python-dotenv could not parse statement starting at line 1 
Python :: Python Changing a Tuple 
Python :: Django delete a session value 
Python :: python find if string contains space 
Python :: how to concatenate in python 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =