如何在python kivy中更改LineRectangle类中的参数?

如何在python kivy中更改LineRectangle类中的参数?,python,label,kivy,box,Python,Label,Kivy,Box,我想在图像上画多框。在PythonKivy中,如何在LineRectangle类中更改box pos、size和lable的参数 class LineRectangle(Widget): def __init__(self, **kwargs): super(LineRectangle, self).__init__(**kwargs) with self.canvas: Color(1, 0, 0, 1)

我想在图像上画多框。在PythonKivy中,如何在LineRectangle类中更改box pos、size和lable的参数

class LineRectangle(Widget):
    def __init__(self, **kwargs):
        super(LineRectangle, self).__init__(**kwargs)
        with self.canvas:
            Color(1, 0, 0, 1)
            self.line = Line(width=2, rectangle=(self.x, self.y, self.width, self.height))
            self.label = Label(text='Rectangle', pos=(self.x, self.y), size=(10, 10))


class LineExtendedApp(App):
    def build(self):
        root = FloatLayout()
        image = Image(source='000001.jpg', allow_stretch=False, keep_ratio=True)
        root.add_widget(image)
        bbox1 = LineRectangle()
        bbox1.line = Line(width=1, rectangle=(100, 100, 100, 100))
        bbox1.label = Label(text='bbox1', pos=(100, 100), size=(10, 10))
        bbox2 = LineRectangle()
        bbox2.line = Line(width=1, rectangle=(300, 300, 100, 100))
        bbox2.label = Label(text='bbox1', pos=(300, 300), size=(10, 10))
        root.add_widget(bbox1)
        root.add_widget(bbox2)
        return root
if __name__ == '__main__':
    LineExtendedApp().run()

您可以为要更改的参数创建属性。然后使用
kv
规则创建绑定(这样对
属性的更改将自动显示在显示器中)。以下是如何做到这一点的示例:

from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import ListProperty, NumericProperty, StringProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.widget import Widget


class LineRectangle(Widget):
    line_color = ListProperty([])
    line_rect = ListProperty([])
    line_width = NumericProperty()
    label_text = StringProperty('')
    label_pos = ListProperty([])

    def __init__(self, **kwargs):
        self.line_color = kwargs.pop('line_color', [1,1,1,1])
        self.line_rect = kwargs.pop('line_rect', [0,0,50,50])
        self.line_width = kwargs.pop('line_width', 1)
        self.label_text = kwargs.pop('label_text', 'text')
        self.label_pos = kwargs.pop('label_pos', [0,0])
        super(LineRectangle, self).__init__()

Builder.load_string('''
<LineRectangle>:
    canvas:
        Color:
            rgba: root.line_color
        Line:
            width: root.line_width
            rectangle: root.line_rect
    Label:
        text: root.label_text
        pos: root.label_pos
        size: 10, 10
''')


class LineExtendedApp(App):
    def build(self):
        root = FloatLayout()
        image = Image(source='000001.jpg', allow_stretch=False, keep_ratio=True)
        root.add_widget(image)
        self.bbox1 = LineRectangle(line_wdth=2, line_rect=[100, 100, 100, 100], line_color=[1,0,0,1], label_text='bbox1', label_pos=[100, 100])
        bbox2 = LineRectangle(line_width=1, line_rect=[300, 300, 300, 300], line_color=[0,1,0,1], label_text='bbox2', label_pos=[300, 300])
        root.add_widget(self.bbox1)
        root.add_widget(bbox2)
        Clock.schedule_once(self.adjust, 2)  # just to demonstrate change parameter
        return root

    def adjust(self, dt):
        self.bbox1.line_width = 10


if __name__ == '__main__':
    LineExtendedApp().run()
从kivy.app导入应用
从kivy.clock导入时钟
从kivy.lang导入生成器
从kivy.properties导入ListProperty、NumericProperty、StringProperty
从kivy.uix.floatlayout导入floatlayout
从kivy.uix.image导入图像
从kivy.uix.widget导入widget
类LineRectangle(小部件):
line_color=ListProperty([])
line_rect=ListProperty([])
线宽=NumericProperty()
label_text=StringProperty(“”)
label_pos=ListProperty([])
定义初始(自我,**kwargs):
self.line\u color=kwargs.pop('line\u color',[1,1,1])
self.line_rect=kwargs.pop('line_rect',[0,0,50,50])
self.line\u width=kwargs.pop('line\u width',1)
self.label\u text=kwargs.pop('label\u text','text'))
self.label\u pos=kwargs.pop('label\u pos',[0,0])
超级(线性矩形,自)。\uuuu初始化
Builder.load_字符串(“”)
:
画布:
颜色:
rgba:root.line\u颜色
行:
宽度:root.line\u宽度
矩形:root.line\u rect
标签:
text:root.label\u text
pos:root.label\u pos
尺码:10,10
''')
类LineExtendedApp(应用程序):
def生成(自):
root=FloatLayout()
image=image(source='000001.jpg',allow\u stretch=False,keep\u ratio=True)
root.add_小部件(图像)
self.bbox1=LineRectangle(line_wdth=2,line_rect=[100100100],line_color=[1,0,0,1],label_text='bbox1',label_pos=[100100])
bbox2=线条矩形(线条宽度=1,线条矩形=[300300300],线条颜色=0,1,0,1],标签文字=bbox2,标签位置=[300300])
root.add_小部件(self.bbox1)
root.add_小部件(bbox2)
时钟。计划一次(自我调整,2)#只是为了演示参数的变化
返回根
def调节(自调节,dt):
self.bbox1.line_宽度=10
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
LineExtendedApp().run()