mirror of https://github.com/kivy/kivy.git
add rst editor in example,
there is still a but when one touch the TextInput, the FileChooser behind get the touch anyway, and thus move, not sure if kivy bug or mine.
This commit is contained in:
parent
e62a20f4c2
commit
b91f28e88d
|
@ -0,0 +1,72 @@
|
|||
#:kivy 1.0.10
|
||||
|
||||
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
|
||||
id: text_input
|
||||
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)
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
#!/usr/bin/env python
|
||||
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):
|
||||
print "load"
|
||||
with open(os.path.join(path, filename[0])) as stream:
|
||||
self.text_input.text = stream.read()
|
||||
|
||||
self.dismiss_popup()
|
||||
|
||||
def save(self, path, filename):
|
||||
print "save"
|
||||
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()
|
||||
|
Loading…
Reference in New Issue