Home > Back-end >  Drawing widget class won't draw when added to existing code Kivy Python
Drawing widget class won't draw when added to existing code Kivy Python

Time:10-01

Everything from MyGridLayout(Widget) works fine. The drawing box for the DrawingWidget shows up, but does not draw a line. If I put the DrawingWidget in it's own solo program it allows me to draw just fine. Is there something wrong with the placement of the class? I have tried many arrangements of the KV document and am only able to get the drawing box to appear below all my other widgets from MyGridLayout that function fine.

class MyGridLayout(Widget):
    def __init__(self, **kwargs):
        super(MyGridLayout, self).__init__(**kwargs)
        pass
class DrawingWidget(Widget):
        def __init__(self, **kwargs):
            super(DrawingWidget, self).__init__(**kwargs)

            with self.canvas:
                Color(1, 1, 1, 1)
                self.rect = Rectangle(size=self.size,
                                    pos=self.pos)
            self.bind(pos=self.update_rectangle,
                    size=self.update_rectangle)

        def update_rectangle(self, instance, value):
            self.rect.pos = self.pos
            self.rect.size = self.size

        def on_touch_down(self, touch):
            super(DrawingWidget, self).on_touch_down(touch)

            if not self.collide_point(*touch.pos):
                return

            with self.canvas:
                Color(0, 0, 0, 1)
                self.line = Line(points=[touch.pos[0], touch.pos[1]], width=2)

        def on_touch_move(self, touch):
            if not self.collide_point(*touch.pos):
                return

            self.line.points = self.line.points   [touch.pos[0], touch.pos[1]]              
                    

class funcswspinnerall(App):
    def build(self):
        return MyGridLayout()


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

And here is an abbreviated KV file


<MyGridLayout>:
    ScrollView:
        size: root.width, root.height
        GridLayout:
            cols:1
            padding:9,9,9,9
          
            GridLayout:
                cols:3
                size_hint_y:.5
                Label:
                Button:
                    text:'Enter Activity'
                    on_press:root.sendmatt()
                    size_hint_x:3
                    size_hint_y:1
                Label:
            GridLayout:
                cols:1
                DrawingWidget:
                    
<DrawingWidget>:
    BoxLayout:
        orientation: 'horizontal'
        size_hint_y: None
        height: 50

CodePudding user response:

The problem is that you have the DrawingWidget inside a ScrollView. The ScrollView wants to use the mouse touches for scrolling, so there is an interference with your drawing. The fix is to either move the DrawingWidget out of the ScrollView or add:

scroll_type: ['bars']

to the ScrollView, which limits your scrolling to using the scroll bars.

  • Related