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

2024-09-30 03:24:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在图像上画多框。在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()

Tags: textposselfaddsizelinerootwidth
1条回答
网友
1楼 · 发布于 2024-09-30 03:24:56

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

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

相关问题 更多 >

    热门问题