Python kivy使用浮动布局将按钮/标签放置在不同的地方

时间:2016-06-27 15:45:00

标签: kivy python-3.4

我是kivy的新手,直到现在我只使用了tkinter,但是我想为Android创建一个应用程序,所以现在我已经转移到了kivy。

我已经看到小部件在kivy上完全不同,无论如何我只是想在屏幕上放置标签和按钮,我已经用其他布局完成了这个但是它说浮动布局有点像tkinter的.place().....所以我认为这可能有用但不幸的是我似乎无法找到如何在屏幕上放置一些东西。这是我想要添加按钮和标签的程序:

from random import random
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse, Line
from kivy.core.window import Window


class MyPaintWidget(Widget):
    Window.clearcolor = (1, 1, 1, 1)

    def on_touch_down(self, touch):
        color = (0,0,0)
        with self.canvas:
            Color(*color, mode='hsv')
            d = 10
            Ellipse(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d))
            touch.ud['line'] = Line(points=(touch.x, touch.y),width=5)

    def on_touch_move(self, touch):
        touch.ud['line'].points += [touch.x, touch.y]


class MyPaintApp(App):

    def build(self):
        parent = Widget()
        self.painter = MyPaintWidget()
        clearbtn = Button(text='Clear')
        clearbtn.bind(on_release=self.clear_canvas)
        parent.add_widget(self.painter)
        parent.add_widget(clearbtn)
        return parent

    def clear_canvas(self, obj):
        self.painter.canvas.clear()


MyPaintApp().run()

这会创建一个绘图屏幕,我想要的是一种模拟的合同'用户可以在其上叹息,但我无法弄清楚如何按下我的选择放置按钮和标签。

1 个答案:

答案 0 :(得分:0)

zeeMonkeez已经回答了这个问题:

您的示例不使用FloatLayout。您是否查看了文档并使用了示例?

所以对于任何有同样问题的人来说都是答案:

from random import random
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse, Line
from kivy.core.window import Window
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput


class MyPaintWidget(Widget):
    Window.clearcolor = (1, 1, 1, 1)

    def on_touch_down(self, touch):
        color = (0,0,0)
        with self.canvas:
            Color(*color, mode='hsv')
            d = 3
            Ellipse(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d))
            touch.ud['line'] = Line(points=(touch.x, touch.y),width=1.5)

    def on_touch_move(self, touch):
        touch.ud['line'].points += [touch.x, touch.y]


class MyPaintApp(App):

    def build(self):
        parent = FloatLayout()
        self.painter = MyPaintWidget()
        a_button = Button(text = 'A button',pos=(20,20),size_hint=(.25,.25))
        entry_name = TextInput()
        parent.add_widget(self.painter)
        parent.add_widget(a_button)
        return parent

    def clear_canvas(self, obj):
        self.painter.canvas.clear()


MyPaintApp().run()