繁体   English   中英

绘图小部件 class 添加到现有代码时不会绘制 Kivy Python

[英]Drawing widget class won't draw when added to existing code Kivy Python

MyGridLayout(Widget) 中的所有内容都可以正常工作。 DrawingWidget 的绘图框出现,但没有画线。 如果我将 DrawingWidget 放在它自己的单独程序中,它可以让我画得很好。 class 的放置是否有问题? 我已经尝试了 KV 文档的许多安排,并且只能让绘图框出现在 MyGridLayout 中的所有其他小部件下方,这些小部件 function 很好。

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

这是一个缩写的KV文件


<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

问题是您在ScrollView中有DrawingWidget ScrollView想要使用鼠标触摸进行滚动,因此会干扰您的绘图。 修复方法是将DrawingWidget移出ScrollView或添加:

scroll_type: ['bars']

ScrollView ,这将您的滚动限制为使用滚动条。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM