Examples: kivycatalog allow for dynamic updation of kv and text highlighting

using code_input.
This commit is contained in:
Qua-non 2012-10-24 00:54:59 +05:30
parent bf1bc3be4d
commit 9f2003e438
5 changed files with 58 additions and 29 deletions

View File

@ -3,16 +3,6 @@ This is the Kivy Catalog viewer. It serves two purposes:
1. To showcase the various widgets available in Kivy
2. To allow interactive editing of Kivy language files
to get immediate feedback as to how they work
To use it, you'll need to install Kivy and it's dependencies
a la http://kivy.org/docs/installation/installation.html Then run
python main.py and browse or edit widgets to your heart's content.
Known bugs:
* The DropDown item I had tested completely crashes Kivy
* The GridLayout example could use some extra features
* If you try to start the app with focused set to true, weird stuff happens.
but it works fine if you set focused to true and press render.
* Video playback doesn't work for me, but this may be a dependency issue
* Popups are displayed inline
* The DropDown
* Some widgets are still missing

View File

@ -16,6 +16,6 @@ GridLayout:
Label:
text: "different font"
bold: True
font_name: "DroidSansMono.ttf"
font_name: "data/fonts/DroidSansMono.ttf"
font_size: 32
valign: "bottom"

View File

@ -1,4 +1,5 @@
#:kivy 1.4
#:import KivyLexer kivy.extras.highlight.KivyLexer
[ContainerToggle@ToggleButton]:
group: "container_toggle"
@ -9,6 +10,9 @@
<Catalog>:
language_box: language_box
screen_manager: screen_manager
auto_reload: chkbx.active
info_label: info_lbl
orientation: 'vertical'
BoxLayout:
BoxLayout:
size_hint: None, 1
@ -108,13 +112,36 @@
size_hint: None, 1
width: 400
KivyRenderTextInput:
lexer: KivyLexer()
text_size: self.width-20, self.height-20
font_name: "DroidSansMono.ttf"
font_name: "data/fonts/DroidSansMono.ttf"
valign: "top"
id: language_box
text: "This box will display the kivy language for whatever has been selected"
Button:
on_text: root.schedule_reload()
BoxLayout:
size_hint: 1, None
height: 50
text: "Render"
on_press: root.change_kv(*args)
height: '30pt'
BoxLayout:
CheckBox:
id: chkbx
active: True
Label:
text: "Auto Reload"
Button:
text: 'Render Now'
on_release: root.change_kv(*args)
FloatLayout:
size_hint: 1, None
height: 0
TextInput:
id:info_lbl
readonly: True
font_size: 11
background_color: (0, 0, 0, 1)
foreground_color: (1, 1, 1, 1)
opacity:0
size_hint: 1, None
text_size: self.size
height: '150pt'
top: 0

View File

@ -11,7 +11,9 @@ from kivy.config import Config
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.codeinput import CodeInput
from kivy.animation import Animation
from kivy.clock import Clock
print Config.get('graphics', 'width')
@ -43,7 +45,9 @@ class Container(BoxLayout):
@property
def kv_file(self):
'''Get the name of the kv file, a lowercase version of the class name.'''
'''Get the name of the kv file, a lowercase version of the class
name.
'''
return os.path.join('container_kvs',
self.__class__.__name__ + ".kv")
@ -52,7 +56,7 @@ for class_name in CONTAINER_CLASSES:
globals()[class_name] = type(class_name, (Container,), {})
class KivyRenderTextInput(TextInput):
class KivyRenderTextInput(CodeInput):
def _keyboard_on_key_down(self, window, keycode, text, modifiers):
is_osx = sys.platform == 'darwin'
# Keycodes on OSX:
@ -75,8 +79,9 @@ class Catalog(BoxLayout):
a tabbed pain of widgets that can be displayed and a textbox where .kv
language files for widgets being demoed can be edited.
The entire interface for the Catalog is defined in kivycatalog.kv, although
individual containers are defined in the container_kvs directory.
The entire interface for the Catalog is defined in kivycatalog.kv,
although individual containers are defined in the container_kvs
directory.
To add a container to the catalog,
first create the .kv file in container_kvs
@ -114,11 +119,17 @@ class Catalog(BoxLayout):
0].kv_file) as file:
self.language_box.text = file.read()
def change_kv(self, button):
def schedule_reload(self):
if self.auto_reload:
Clock.unschedule(self.change_kv)
Clock.schedule_once(self.change_kv, 2)
def change_kv(self, *largs):
'''Called when the update button is clicked. Needs to update the
interface for the currently active kv widget, if there is one based
on the kv file the user entered. If there is an error in their kv
syntax, show a nice popup.'''
kv_container = self.screen_manager.current_screen.content.children[0]
try:
parser = Parser(content=self.language_box.text.encode('utf8'))
@ -127,16 +138,17 @@ class Catalog(BoxLayout):
Builder._apply_rule(widget, parser.root, parser.root)
kv_container.add_widget(widget)
except (SyntaxError, ParserException) as e:
content = Label(text=str(e), text_size=(350, None))
popup = Popup(title="Parse Error in Kivy Language Markup",
content=content, text_size=(350, None),
size_hint=(None, None), size=(400, 400))
popup.open()
self.info_label.text = str(e)
self.anim = Animation(top=190.0, opacity=1, d=2, t='in_back') +\
Animation(top=190.0, d=2) +\
Animation(top=0, opacity=0, d=2)
self.anim.start(self.info_label)
except:
import traceback
traceback.print_exc()
popup = Popup(title="Boom",
content=Label(text="Something horrible happened while parsing your Kivy Language", text_size=(350, None)),
content=Label(text='Something horrible happened while parsing'
+ 'your Kivy Language", text_size=(350, None)),
text_size=(350, None),
size_hint=(None, None), size=(400, 400))
popup.open()