Kivy:坐标

时间:2017-12-14 22:19:12

标签: python kivy

在下面的代码中,我没有解释两个问题:

  1. 如果我使用方法__init__ - > Scene.canvas =无
  2. 我不会对图像中的相对位置对象感兴趣。
  3. 我想从Kivy手册获得有关坐标系统的知识,但没有成功。

    from kivy.app import App
    from kivy.uix.widget import Widget
    from kivy.core.window import Window
    from kivy.lang import Builder
    from kivy.uix.image import Image    
    from kivy.graphics import Ellipse
    
    class Scene(Widget):
        """
        def __init__(self):
            self.x = 0
            self.y = 100
            self.dir = 1
        """
        def set_par(self):
            self.x = 0
            self.y = 100
            self.dir = 1
    
        def create_ball(self):
            with self.canvas:
                Ellipse(pos = (0,0), size = (40, 40))
            img = Image(pos = (0,0), source = 'image1.png') # square 40x40pixels  
            self.add_widget(img)          
    
    class SceneApp(App):
        def build(self):
            scene = Scene()
            scene.create_ball()
    
            return scene
    
    if __name__ == '__main__':
        SceneApp().run()
    

    Printscreen形成我的代码: enter image description here

1 个答案:

答案 0 :(得分:0)

您未在super()方法中致电__init__。因此,Widget __init__方法会被彻底改写 然后使用FloatLayout代替Widget
你可以做这样的事情:

from kivy.app import App
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.image import Image
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Ellipse

Window.size = (300, 300)

class Scene(FloatLayout):

    def __init__(self, **kwargs):
        super(Scene,self).__init__(**kwargs)
        self.size = 300,300

    def create_ball(self):
        img = Image(pos = (-130, -130), source = 'image1.png') # square 40x40pixels
        self.add_widget(img)
        with self.canvas:
            Ellipse(pos = (0,0), size = (40, 40))

class SceneApp(App):
    def build(self):
        scene = Scene()
        scene.create_ball()
        return scene

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