[英]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.