我想在FloatLayout的中心绘制圆圈。据我所知,我仅获得了默认值。为什么显示的代码中的圆圈不是红色?您能解释一下获取必要坐标的过程吗?

import kivy
from kivy.config import Config
kivy.config.Config.set('graphics','resizable', False)


from kivy.app import App
from kivy.graphics import Color, Ellipse

from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button



class Scene(FloatLayout):
    def __init__(self, **kwargs):
        super(Scene, self).__init__(**kwargs)

    def draw_circle(self):
        with self.canvas:
            Color=(1,0,0)
            circ = Ellipse(pos = (self.center_x, self.center_y), size=(20,20))

    def on_touch_down(self, touch):
        pass

class Game(BoxLayout):
    def __init__ (self,**kwargs):
        super(Game, self).__init__(**kwargs)
        self.orientation = 'vertical'

        but1 = Button(text = 'button 1')
        self.add_widget(but1)

        self.scene =  Scene()
        self.add_widget(self.scene)


class TestApp(App):
    def build(self):
        game = Game()
        game.scene.draw_circle()

        return game

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

最佳答案

创建浮动布局时,应定义其大小。

self.scene =  Scene(size=(300, 300))


然后,您的圆应位于FloatLayout尺寸的中心。

我还认为FloatLayout最好与size_hintpos_hint而不是固定坐标一起使用。

09-09 21:20