绘制按钮问题

时间:2016-03-04 17:32:37

标签: python kivy

我有一个绘图应用程序的kivy代码。

<PaintApp>:
    name: "PaintApp"
    BoxLayout:
        orientation:'vertical'
        CanvasWidget:
            size_hint_y:.9
        BoxLayout:
            size_hint:(1,None)
            size_hint_y:.1
            orientation: 'horizontal'
            Button:
                text: 'Delete'
                font_size: 20
              # on_release: root.clear_canvas()
                background_normal: 'red_button_normal.png'
                background_down: 'red_button_down.png'
                border: (2, 2, 2, 2)
                right: root.right
                top: root.top
                width: 80
                height: 40
            Button:
                text: 'Back'
                font_size: 20
              # on_release: app.root.current= 'ActivitySelectScreen'
                background_normal: 'red_button_normal.png'
                background_down: 'red_button_down.png'
                border: (2, 2, 2, 2)
                right: root.right -80
                top: root.top
                width: 80
                height: 40

            LineWidthButton:
                text: 'Thin'

            LineWidthButton:
                text: 'Normal'
                state: 'down'

            LineWidthButton:
                text: 'Thick'


            ColorButton:
                background_color: C('#2980b9')
                state: 'down'

            ColorButton:
                background_color: C('#16a085')

            ColorButton:
                background_color: C('#27ae60')

            ColorButton:
                background_color: C('#f39c12')

            ColorButton:
                background_color: C('#d35400')

            ColorButton:
                background_color: C('#c0392b')

            ColorButton:
                background_color: C('#8e44ad')

            ColorButton:
                background_color: C('#bdc3c7')

            ColorButton:
                background_color: C('#7f8c8d')

            ColorButton:
                background_color: C('#2c3e50')

Python代码:

class CanvasWidget(Widget):
    line_width = 2
    def on_touch_down(self, touch):
        if Widget.on_touch_down(self, touch):
            return

        with self.canvas:
            touch.ud['current_line'] = Line(
                points=(touch.x, touch.y),
                width=self.line_width)

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

    def set_color(self, new_color):
        self.last_color = new_color
        self.canvas.add(Color(*new_color))

    def set_line_width(self, line_width='Normal'):
        self.line_width = {'Thin': 1, 'Normal': 2, 'Thick': 4}[line_width]

    def clear_canvas(self):
        saved = self.children[:]
        self.clear_widgets()
        self.canvas.clear()
        for widget in saved:
            self.add_widget(widget)

输出应用程序如下所示:

它也以某种方式吸引了按钮。我不明白为什么。当我在框布局中添加两个按钮时。它不会在另一个上面添加一个。

1 个答案:

答案 0 :(得分:2)

第一件事:代码不完整,LineWidthButtonColorButton未在python代码中定义,即使在kv文件中也没有,它们只是使用。各种东西都缺少进口。当你提出问题时不要这样做......而且,这种行为不是问题。

除了我无法运行它,我可以从你的描述中看出,你的CanvasWidget肯定会使用整个画布而不仅仅是小部件的大小/空间,即图形将显示在整个屏幕上。对此的“修复”是StencilView。您可以在this demo中清楚地看到图纸留在边界框内。

按钮,它是一个BoxLayout,位于BoxLayout的{​​{3}}页面顶部,您有一个显示布局行为的gif:

docs

如果您想将Button放在另一个按钮上,则还有其他布局可以采用这种方式。例如FloatLayout

enter image description here

相关问题