【问题标题】:Python kivy placing buttons/labels in different places using float layoutPython kivy 使用浮动布局将按钮/标签放置在不同的位置
【发布时间】:2016-06-27 15:44:59
【问题描述】:

我是 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()

这会创建一个绘图屏幕,我想要的是一种模拟的“合同”,用户可以在上面叹息,但我不知道如何根据自己的选择放置按钮和标签。

【问题讨论】:

  • 您的示例没有使用FloatLayout。你看过the documentation 并玩过这些例子吗?
  • 我没有使用布局,因为我看过文档并且没有描述如何使用 .place()
  • 哦,对不起,我刚刚看到你使用,pos = ()
  • 是否可以在 Widget 上使用浮动布局?
  • 一个FloatLayout 一个Widget。开始咨询Widgets and Layouts...

标签: kivy python-3.4


【解决方案1】:

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()

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 2021-09-24
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多