使用像按钮一样的 kivy 图像

using kivy image like buttons

我试图用图像制作一个 kivy 程序,它可以做一些事情(打印按摩),但它不起作用。 我什至没有收到错误消息,我得到的只是黑屏。 好像打开了程序,但是没有显示图片

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.clock import Clock
from kivy.animation import Animation
from kivy.properties import ListProperty
from kivy.properties import NumericProperty

root_widget = Builder.load_string('''
<Root>:
    canvas.before:
        Color:
            rgba: 1, 1, 1, 1
    Knopf:
        pos: 400, 100
    BILD1:
        pos: root.Width/4, root.Height/4
    BILD2:
        pos: root.Width/2, root.Height/4
    BILD3:
        pos: root.Width/4, root.Height/2
    BILD4:
        pos: root.Width/2, root.Height/2
<Knopf>:
    Image:
        pos: root.pos
        id: my_image
        source: 'bestatigung.png'
<BILD1>:
    Image:
        pos: root.pos
        id: pic1
        source: 'Bild1.png'
<BILD2>:
    Image:
        pos: root.pos
        id: pic2
        source: 'Bild2.png'
<BILD3>:
    Image:
        pos: root.pos
        id: pic3
        source: 'Bild3.png'
<BILD4>:
    Image:
        pos: root.pos
        id: pic4
        source: 'Bild4.png'
''')
class Root(Widget):
    pass

class Knopf(Widget):
    Width = NumericProperty(Window.width)
    Height = NumericProperty(Window.height)
    velocity = ListProperty([1, 0])

    def __init__(self, **kwargs):
        super(Knopf, self).__init__(**kwargs)
        Clock.schedule_interval(self.Update, 1/60.)

    def Update(self, *args):
        pass

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            print 'es geht'
            
class BILD1(Knopf):
    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            print 'es geht'
class BILD2(Knopf):
    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            print 'es geht'
class BILD3(Knopf):
    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            print Window.width
class BILD4(Knopf):
    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            print Window.height
class app(App):
    def build(self):
        return root_widget

if __name__ == "__main__":
    app().run()

您的 kv 字符串未定义根小部件,因此 root_widget 只是 None。

你可以改为

def build(self):
        return Root()