【问题标题】:Kivy draw on a widgetKivy 在小部件上绘制
【发布时间】:2013-09-07 21:21:54
【问题描述】:

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

  1. 几个按钮
  2. MyPaintWidget

在 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 一样。还有为什么我的按钮在你点击的时候会显示出来。

问候

【问题讨论】:

    标签: python widget kivy


    【解决方案1】:

    解决办法是修改布局文件

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 2021-12-03
      • 1970-01-01
      相关资源
      最近更新 更多