I am very new to Kivy, and have found an online example for a simple calculator here that I would like to try to modify using the Kivy examples ~/examples/keyboard/main.py. In that example is a numeric keyboard, but I can't seem to figure out how to get it to appear instead of the standard keyboard. Help or suggestions would be greatly appreciated.
Here is what I have started with:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Window
from kivy.uix.vkeyboard import VKeyboard
from kivy.properties import ObjectProperty
from kivy.uix.button import Button
from kivy.config import Config
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy import require
Builder.load_string("""
<KeyboardScreen>:
displayLabel: displayLabel
kbContainer: kbContainer
BoxLayout:
orientation: 'vertical'
Label:
size_hint_y: 0.15
text: "Available Keyboard Layouts"
BoxLayout:
id: kbContainer
size_hint_y: 0.2
orientation:"horizontal"
padding: 10
Label:
id: displayLabel
size_hint_y: 0.15
markup: True
text: "[b]Key pressed[/b] - None"
halign: "center"
Button:
text: "Back"
size_hint_y: 0.1
on_release: root.manager.current = "mode"
Widget:
# Just a space taker to allow for the popup keyboard
size_hint_y: 0.5
<Calc>:
# This are attributes of the class Calc now
a: _a
b: _b
result: _result
KeyboardScreen:
AnchorLayout:
anchor_x: 'center'
anchor_y: 'top'
ScreenManager:
size_hint: 1, .9
id: _screen_manager
Screen:
name: 'screen1'
GridLayout:
cols:1
# TextInput:
Button:
id: _a
text: '3'
on_press:KeyboardScreen(name="keyboard")
# TextInput:
Button:
id: _b
text: '5'
on_press:KeyboardScreen(name="keyboard")
Label:
id: _result
Button:
text: 'sum'
# You can do the opertion directly
on_press: _result.text = str(int(_a.text) + int(_b.text))
Button:
text: 'product'
# Or you can call a method from the root class (instance of calc)
on_press: root.product(*args)
Screen:
name: 'screen2'
Label:
text: 'The second screen'
AnchorLayout:
anchor_x: 'center'
anchor_y: 'bottom'
BoxLayout:
orientation: 'horizontal'
size_hint: 1, .1
Button:
text: 'Go to Screen 1'
on_press: _screen_manager.current = 'screen1'
Button:
text: 'Go to Screen 2'
on_press: _screen_manager.current = 'screen2'""")
class Calc(FloatLayout):
# define the multiplication of a function
def product(self, instance):
# self.result, self.a and self.b where defined explicitely in the kv
self.result.text = str(int(self.a.text) * int(self.b.text))
class KeyboardScreen(Screen):
def __init__(self, **kwargs):
super(KeyboardScreen, self).__init__(**kwargs)
# self._add_keyboards()
self.set_layout("numeric.json","numeric.json")
self._keyboard = None
def set_layout(self, layout, button):
""" Change the keyboard layout to the one specified by *layout*. """
kb = Window.request_keyboard(
self._keyboard_close, self)
if kb.widget:
# If the current configuration supports Virtual Keyboards, this
# widget will be a kivy.uix.vkeyboard.VKeyboard instance.
self._keyboard = kb.widget
self._keyboard.layout = layout
else:
self._keyboard = kb
self._keyboard.bind(on_key_down=self.key_down,on_key_up=self.key_up)
def _keyboard_close(self, *args):
""" The active keyboard is being closed. """
if self._keyboard:
self._keyboard.unbind(on_key_down=self.key_down)
self._keyboard.unbind(on_key_up=self.key_up)
self._keyboard = None
def key_down(self, keyboard, keycode, text, modifiers):
""" The callback function that catches keyboard events. """
self.displayLabel.text = u"Key pressed - {0}".format(text)
# def key_up(self, keyboard, keycode):
def key_up(self, keyboard, keycode, *args):
""" The callback function that catches keyboard events. """
# system keyboard keycode: (122, 'z')
# dock keyboard keycode: 'z'
if isinstance(keycode, tuple):
keycode = keycode[1]
self.displayLabel.text += u" (up {0})".format(keycode)
class TestApp(App):
sm = None # The root screen manager
Config.set("kivy", "keyboard_mode", 'dock')
Config.write()
def build(self):
# self.sm = ScreenManager()
# self.sm.add_widget(KeyboardScreen(name="keyboard"))
return Calc()
if __name__ == '__main__':
TestApp().run()