使用像按钮的kivy图像

时间:2015-01-31 20:18:34

标签: python-2.7 kivy

我试图制作一个带有图像的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()

1 个答案:

答案 0 :(得分:0)

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

你可以改为

def build(self):
        return Root()