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

时间:2021-12-19 00:08:22

I am learning using kivy right now on my Raspberry-Pi. I installed the most recent kivypie image and I do want to make a simle app, which changes an image content and some label on buttonpress and keypress.

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

The buttonpress works as expected, but the after pressing uo/down keys on the keyboard only the label text changes and no image is being displayed.

按钮按预期工作,但按下键盘上的u / down键后,标签文本只会更改,并且不显示任何图像。

Also I can quit the App pressing the q button but not the escape button as I would like.

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

Here is my current code:

这是我目前的代码:

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 个解决方案

#1


If you uncomment your print statements, you'll see the information you are looking for is in keycode, not text. text will only match lettered keys, for special keys (escape, up, down, etc) it will not. Try changing it such that:

如果取消注释打印语句,您将看到您要查找的信息是键码而不是文本。文本只匹配字母键,对于特殊键(转义,向上,向下等),它不会。尝试更改它,以便:

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

#1


If you uncomment your print statements, you'll see the information you are looking for is in keycode, not text. text will only match lettered keys, for special keys (escape, up, down, etc) it will not. Try changing it such that:

如果取消注释打印语句,您将看到您要查找的信息是键码而不是文本。文本只匹配字母键,对于特殊键(转义,向上,向下等),它不会。尝试更改它,以便:

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