我有一个使用全屏的Kivy应用程序,因此我从未使用过Window对象。我现在想添加一个弹出键盘,可以在必要时隐藏和弹出。但是,看看如何做到这一点似乎我需要一个Window对象才能存在。
所以我添加了from kivy.core.window import Window
以及在Kivy网站上的窗口文档中复制键盘示例。
然而,现在我的全屏'应用程序占据屏幕的一部分,键盘无处可见?我错过了什么?
添加了运行代码:
from kivy.config import Config
Config.set('graphics', 'borderless', '1')
Config.set('kivy', 'keyboard_mode', 'systemandmulti')
Config.set('graphics', 'width', '1920')
Config.set('graphics', 'height', '1080')
Config.set('graphics', 'fullscreen', 'auto')
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.vkeyboard import VKeyboard
from kivy.base import runTouchApp
Builder.load_file("mykivyfile.kv")
class Widgets(Widget):
pass
class MyKeyboardListener(Widget):
def __init__(self, **kwargs):
super(MyKeyboardListener, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(
self._keyboard_closed, self, 'text')
if self._keyboard.widget:
# If it exists, this widget is a VKeyboard object which you can use
# to change the keyboard layout.
pass
self._keyboard.bind(on_key_down=self._on_keyboard_down)
def _keyboard_closed(self):
print('My keyboard have been closed!')
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
self._keyboard = None
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
print('The key', keycode, 'have been pressed')
print(' - text is %r' % text)
print(' - modifiers are %r' % modifiers)
# Keycode is composed of an integer + a string
# If we hit escape, release the keyboard
if keycode[1] == 'escape':
keyboard.release()
# Return True to accept the key. Otherwise, it will be used by
# the system.
return True
class myApp(App):
def build(self):
global w
w = Widgets()
return w
def testy(self):
runTouchApp(MyKeyboardListener())
def quitme(self):
quit()
if __name__ == "__main__":
myApp().run()
mykivyfile.kv:
#:kivy 1.8.0
<Widgets>:
Button:
size: 100,100
pos: 100,100
text: 'test'
on_press: app.testy()
Button:
size: 100,100
pos: 200,100
text: 'quit'
on_press: app.quitme()