Kivy安卓实战:5 |Fasta 编辑器| 寫個工具箱把5 - 图1
© Karobben

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

  1. CryptoWatch-Kivy 1.13
  2. Kivy 2.0.0
  3. Kivy-Garden 0.1.4
  4. kivy-garden.wordcloud 1.0.0
  5. kivymd 0.104.2.dev0

Quick Review

  1. tree
  1. .
  2. ├── bin
  3. └── KarobbenTB-1.3-armeabi-v7a-debug.apk
  4. ├── buildozer.spec
  5. ├── favicon.ico
  6. ├── font
  7. ├── ArtificialBox-WdD4.ttf
  8. | ...
  9. └── JingDianFanJiaoZhuan-1.ttf
  10. ├── Layout
  11. ├── filechooser.kv
  12. ├── menu.kv
  13. ├── Navigation_Draw.kv
  14. ├── Navigation_Tabs.kv
  15. └── Seq.kv
  16. ├── lib
  17. └── bio_seq.py
  18. ├── libWidget
  19. ├── filechooser.py
  20. ├── main.py
  21. ├── menu.py
  22. └── Seq.py
  23. ├── logo.png
  24. └── main.py

Function for Close Tab

Raw file

Added to widget

  1. touch libWidget/editor.py
  2. touch libWidget/editor.kv

editor.py

  1. from kivy.lang import Builder
  2. from kivy.uix.popup import Popup
  3. from kivy.uix.floatlayout import FloatLayout
  4. from kivy.uix.screenmanager import Screen
  5. from kivy.properties import ObjectProperty
  6. from kivy.utils import platform
  7. import os
  8. class LoadDialog(FloatLayout):
  9. load = ObjectProperty(None)
  10. cancel = ObjectProperty(None)
  11. class SaveDialog(FloatLayout):
  12. save = ObjectProperty(None)
  13. text_input = ObjectProperty(None)
  14. cancel = ObjectProperty(None)
  15. class FunctionWidget():
  16. def main(self):
  17. self.popup = Popup(title="Select .zip file",
  18. content=None,
  19. size_hint=(None, None),
  20. size=(500, 500),
  21. auto_dismiss=True)
  22. Builder.unload_file("Layout/editor.kv")
  23. self.Function_page = Builder.load_file("Layout/editor.kv")
  24. # select a file
  25. self.Function_page.ids.button_load.on_release = self.show_load
  26. # save the file
  27. self.Function_page.ids.button_save.on_release = self.show_save
  28. return self.Function_page
  29. def dismiss_popup(self):
  30. self._popup.dismiss()
  31. def show_load(self):
  32. content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
  33. PATH = "."
  34. if platform == "android":
  35. from android.permissions import request_permissions, Permission
  36. request_permissions([Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE])
  37. app_folder = os.path.dirname(os.path.abspath(__file__))
  38. PATH = "/storage/emulated/0" #app_folder
  39. content.ids.filechooser.path = PATH
  40. self._popup = Popup(title="Load file", content=content,
  41. size_hint=(0.9, 0.9))
  42. self._popup.open()
  43. def show_save(self):
  44. content = SaveDialog(save=self.save, cancel=self.dismiss_popup)
  45. PATH = "."
  46. if platform == "android":
  47. from android.permissions import request_permissions, Permission
  48. request_permissions([Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE])
  49. app_folder = os.path.dirname(os.path.abspath(__file__))
  50. PATH = "/storage/emulated/0" #app_folder
  51. content.ids.filechooser.path = PATH
  52. self._popup = Popup(title="Save file", content=content,
  53. size_hint=(0.9, 0.9))
  54. self._popup.open()
  55. def load(self, path, filename):
  56. with open(os.path.join(path, filename[0])) as stream:
  57. try:
  58. self.Function_page.ids.text_input.text = stream.read()
  59. except:
  60. self.Function_page.ids.text_input.text = "Error, can't open this file"
  61. self.dismiss_popup()
  62. def save(self, path, filename):
  63. with open(os.path.join(path, filename), 'w') as stream:
  64. stream.write(self.Function_page.ids.text_input.text)
  65. self.dismiss_popup()

editor.kv

  1. BoxLayout:
  2. orientation: 'vertical'
  3. BoxLayout:
  4. size_hint_y: None
  5. height: 30
  6. Button:
  7. id: button_load
  8. text: 'Load'
  9. Button:
  10. id: button_save
  11. text: 'Save'
  12. BoxLayout:
  13. TextInput:
  14. id: text_input
  15. text: ''
  16. <LoadDialog>:
  17. BoxLayout:
  18. size: root.size
  19. pos: root.pos
  20. orientation: "vertical"
  21. FileChooserListView:
  22. id: filechooser
  23. path: "."
  24. BoxLayout:
  25. size_hint_y: None
  26. height: 30
  27. Button:
  28. id: test
  29. text: "Cancel"
  30. on_release: root.cancel()
  31. Button:
  32. text: "Load"
  33. on_release: root.load(filechooser.path, filechooser.selection)
  34. <SaveDialog>:
  35. text_input: text_input
  36. BoxLayout:
  37. size: root.size
  38. pos: root.pos
  39. orientation: "vertical"
  40. FileChooserListView:
  41. id: filechooser
  42. on_selection: text_input.text = self.selection and self.selection[0] or ''
  43. TextInput:
  44. id: text_input
  45. size_hint_y: None
  46. height: 30
  47. multiline: False
  48. BoxLayout:
  49. size_hint_y: None
  50. height: 30
  51. Button:
  52. text: "Cancel"
  53. on_release: root.cancel()
  54. Button:
  55. text: "Save"
  56. on_release: root.save(filechooser.path, text_input.text)

Enjoy~

本文由Python腳本GitHub/語雀自動更新

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗