0

我是 Kivy 的新手,完成教程后,我的下一步是将两个教程的小部件添加到一个应用程序中。CombWidget 类将是我的 Widget,Paint 和 PingPong 小部件将添加到其中。对于中间步骤,添加了 BoxLayout 并

  1. 几个按钮
  2. 我的油漆小部件

在 BoxLayout 中。

为了将绘图限制为 MyPaintWidget,添加了一个 if 语句

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

这条线只画在按钮上方的一个小点上。除了按钮之外,点在任何地方都绘制。按钮也不再对点击做出反应。

编码:

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

class CombWidget(Widget):
    pass
class MyPaintWidget(Widget):

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

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

class MyPaintApp(App):       
    def build(self):
        return CombWidget()

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

和布局文件:

#:kivy 1.7.0


<CombWidget>:
    BoxLayout:
        orientation: 'vertical'
        padding: 20
        spacing: 50

        MyPaintWidget:
            size: 100000, 100000
            size_hint: 100000, 100000

        Button:
            text: "Hallo"
        Button:
            text: "Hallo 1"
        Button:
            text: "Hallo 2"

为了增加 MyPaintWidget 的大小,我在 kv 文件中使用了 size: 和 size_hint: 参数,但没有成功。

任何人都可以帮助增加 MyPaintWidget 的大小,以便该区域的行为就像教程中的 MyPaintyApp 一样。还有为什么我的按钮在您单击它们时会显示。

问候

4

1 回答 1

1

解决方案是修改布局文件

<CombWidget>:
BoxLayout:
    orientation: 'vertical'
    size: root.size

还通过在 on_touch_down 和 on_touch_move 方法中添加“return True”,一切正常。

于 2013-09-08T06:36:29.563 回答