from kivy.lang import Builder from kivy.uix.popup import Popup from kivy.uix.floatlayout import FloatLayout from kivy.uix.screenmanager import Screen from kivy.properties import ObjectProperty from kivy.utils import platform 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 FunctionWidget():
def main(self): self.popup = Popup(title="Select .zip file", content=None, size_hint=(None, None), size=(500, 500), auto_dismiss=True)
Builder.unload_file("Layout/editor.kv") self.Function_page = Builder.load_file("Layout/editor.kv") self.Function_page.ids.button_load.on_release = self.show_load self.Function_page.ids.button_save.on_release = self.show_save
return self.Function_page
def dismiss_popup(self): self._popup.dismiss()
def show_load(self): content = LoadDialog(load=self.load, cancel=self.dismiss_popup) PATH = "." if platform == "android": from android.permissions import request_permissions, Permission request_permissions([Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE]) app_folder = os.path.dirname(os.path.abspath(__file__)) PATH = "/storage/emulated/0" content.ids.filechooser.path = PATH
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) PATH = "." if platform == "android": from android.permissions import request_permissions, Permission request_permissions([Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE]) app_folder = os.path.dirname(os.path.abspath(__file__)) PATH = "/storage/emulated/0" content.ids.filechooser.path = PATH 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: try: self.Function_page.ids.text_input.text = stream.read() except: self.Function_page.ids.text_input.text = "Error, can't open this file"
self.dismiss_popup()
def save(self, path, filename): with open(os.path.join(path, filename), 'w') as stream: stream.write(self.Function_page.ids.text_input.text)
self.dismiss_popup()
|