我慢慢地了解了kivy的手册。我需要在FloatLayout中居中图像我根据建议设置FloatLayout的大小:Kivy: Image sometimes is not centered in BoxLayout。图像现在有时位于FloatLayout中心。中心位置图像取决于参数方向的值。参数'值的影响在这里描述:
感谢您的帮助!
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.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
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(self):
img = Image(pos = (0, 0), source = 'grase40.png') # square 40x40pixels
self.add_widget(img)
with self.canvas:
Color=(1,0,0)
Ellipse(pos = (80,80), size = (40, 40))
class Title(BoxLayout):
def __init__(self,**kwargs):
super(Title, self).__init__(**kwargs)
self.orientation ='vertical'
self.scene = Scene()
self.add_widget(self.scene)
but = Button(text ='hallo', size_hint = (0.2, 1))
self.add_widget(but)
class SceneApp(App):
def build(self):
title = Title()
title.scene.create()
return title
if __name__ == '__main__':
SceneApp().run()