在kivy上更改按键上的图像和标签

时间:2015-05-06 20:56:59

标签: python keypress kivy keyboard-events

我正在学习在我的Raspberry-Pi上使用kivy。我安装了最新的kivypie图像,我确实想制作一个simle应用程序,它可以改变按钮和按键上的图像内容和一些标签。

按钮按预期工作,但按下键盘上的u / down键后,只有标签文字发生变化,没有显示图像。

此外,我可以按q按钮而不是我想要的退出按钮退出应用程序。

这是我目前的代码:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.core.window import Window


class MyApp(App):

    def build(self):
        self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
        self._keyboard.bind(on_key_down=self._on_keyboard_down)

        root = BoxLayout(orientation='vertical')
        self.image = Image(source='test.png',
                       allow_stretch=True,
                       keep_ratio=True)
        root.add_widget(self.image)
        self.label = Label(text='Some long and very explanatory text. This is a representation of a custom image description'
                      ' coming with the image. This text can split over several lines and will fit in a box'
                      'defined by the text_size property.',
                      font_size=28,
                      text_size=(600, None),
                      color=(0, 1, 1, 1),
                      size_hint=(1, .2))
        root.add_widget(self.label)
        button = Button(text="Change",
                    size_hint=(1, .07))
        button.bind(on_press=self.callback)

        root.add_widget(button)

        return root

    def callback(self, value):
        self.image.source = 'test.jpg'
        self.label.text = 'No text'

    def _keyboard_closed(self):
        self._keyboard.unbind(on_key_down=self._on_keyboard_down)
        self._keyboard = None

    def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
        #print('### ----------------------------------- ###')
        #print('The key', keycode, 'have been pressed')
        #print(' - text is %r' % text)
        #print(' - modifiers are %r' % modifiers)

        if text == 'escape':
            App.get_running_app().stop()
            #keyboard.release()
        elif text == 'q':
            App.get_running_app().stop()
            #keyboard.release()
        elif text == 'up':
            self.image.source = 'test.jpg'
            self.label.text = 'No text'
            #keyboard.release()
        elif text == 'down':
            self.image.source = 'test.jpg'
            self.label.text = 'No text'
            #keyboard.release()
        return True


if __name__ == '__main__':
    MyApp().run()

1 个答案:

答案 0 :(得分:0)

如果您取消注释打印报表,则会看到您要查找的信息位于keycode,而不是texttext只匹配字母键,对于特殊键(转义,向上,向下等),它不会。尝试更改它:

def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
    print('### ----------------------------------- ###')
    print('The key', keycode, 'have been pressed')
    print(' - text is %r' % text)
    print(' - modifiers are %r' % modifiers)

    key = keycode[1]
    if key == 'escape':
        App.get_running_app().stop()
        #keyboard.release()
    elif key == 'q':
        App.get_running_app().stop()
        #keyboard.release()
    elif key == 'up':
        self.image.source = 'test2.png'
        self.label.text = 'No text'
        #keyboard.release()
    elif key == 'down':
        self.image.source = 'test2.png'
        self.label.text = 'No text'
        #keyboard.release()
    return True