【问题标题】:KIVY change widget size based on another widget positionKIVY 根据另一个小部件位置更改小部件大小
【发布时间】:2020-04-19 21:33:44
【问题描述】:

我在 Relativelayout 中有一条水平线 (hLine) 和一条垂直线 (vLine)。水平线将刚好接触垂直线。当我移动垂直线时,如何动态更改水平线宽度(即水平线将拉伸而不是移动)使其刚好接触垂直线。请有任何想法。在下面给定的示例中,假设垂直线只是从左到右滚动,反之亦然(即它水平移动)。 对于示例,我只提供了一条水平线。在我的实际项目中,至少有 10 条水平线接触垂直线。

from kivy.app import App
from kivy.graphics import Line, Color
from kivy.uix.scatter import Scatter
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.core.window import Window

class MyPaintApp(App):

    def build(self):
        root = RelativeLayout()

        (ix, iy) = (100,100)
        (fx, fy) = (200,100)

        clr = Color(0.2, 0.2, 1)

        wdgt1 = Scatter(pos = (ix,iy), size = (fx-ix, 5))
        (ix,iy) = wdgt1.to_local(ix,iy,relative=True)
        (fx,fy) = wdgt1.to_local(fx, fy,relative=True)
        hLine = Line(points=[ix,iy, fx, fy], width=2, cap='none')
        lbl = Label(text='[color=3333ff]Horizontal[/color]', markup = True, pos=(ix,iy ))
        wdgt1.canvas.add(clr)
        wdgt1.canvas.add(hLine)
        wdgt1.add_widget(lbl)

        (fx, fy) = (200,150)
        (dx, dy) = (200,50)

        wdgt2 = Scatter(pos = (fx,fy), size = (5, fy - dy))
        (fx,fy) = wdgt2.to_local(fx, fy,relative=True)
        (dx,dy) = wdgt2.to_local(dx,dy,relative=True)
        vLine = Line(points=[fx,fy, dx, dy], width=2, cap='none')
        lbl = Label(text='[color=3333ff]Vertical[/color]', markup = True, pos=(fx,fy ))
        wdgt2.canvas.add(clr)
        wdgt2.canvas.add(vLine)
        wdgt2.add_widget(lbl)





        root.add_widget(wdgt1)
        root.add_widget(wdgt2)

        return root

if __name__ == '__main__':
    Window.clearcolor = (1, 1, 1, 1)
    MyPaintApp().run()

【问题讨论】:

    标签: widget kivy


    【解决方案1】:

    您可以通过在wdgt2 移动时重绘wdgt1Canvas 来做到这一点。这是您的代码的修改版本:

    from kivy.app import App
    from kivy.graphics import Line, Color
    from kivy.uix.scatter import Scatter
    from kivy.uix.relativelayout import RelativeLayout
    from kivy.uix.label import Label
    from kivy.core.window import Window
    
    class MyPaintApp(App):
        def __init__(self, **kwargs):
            super(MyPaintApp, self).__init__(**kwargs)
            self.hLine = None
    
        def build(self):
            root = RelativeLayout()
    
            (ix, iy) = (100,100)
            (fx, fy) = (200,100)
    
            self.clr = Color(0.2, 0.2, 1)
    
            self.wdgt1 = Scatter(pos = (ix,iy), size = (fx-ix, 5))
            (ix,iy) = self.wdgt1.to_local(ix,iy,relative=True)
            (fx,fy) = self.wdgt1.to_local(fx, fy,relative=True)
            self.hLine = Line(points=[ix,iy, fx, fy], width=2, cap='none')
            self.lbl = Label(text='[color=3333ff]Horizontal[/color]', markup = True, pos=(ix,iy ))
            self.wdgt1.canvas.add(self.clr)
            self.wdgt1.canvas.add(self.hLine)
            self.wdgt1.add_widget(self.lbl)
    
            (fx, fy) = (200,150)
            (dx, dy) = (200,50)
    
            wdgt2 = Scatter(pos = (fx,fy), size = (5, fy - dy))
            (fx,fy) = wdgt2.to_local(fx, fy,relative=True)
            (dx,dy) = wdgt2.to_local(dx,dy,relative=True)
            vLine = Line(points=[fx,fy, dx, dy], width=2, cap='none')
            lbl = Label(text='[color=3333ff]Vertical[/color]', markup = True, pos=(fx,fy ))
            wdgt2.canvas.add(self.clr)
            wdgt2.canvas.add(vLine)
            wdgt2.add_widget(lbl)
    
            wdgt2.bind(pos=self.move_wdgt2)  # bind to movement of wdgt2
    
            root.add_widget(self.wdgt1)
            root.add_widget(wdgt2)
    
            return root
    
        def move_wdgt2(self, wdgt2, new_pos):
            if self.hLine is None:
                return
    
            # calculate the new ending x coordinate of the hLine
            x1, y1 = self.wdgt1.to_local(wdgt2.x, wdgt2.y, relative=True)
            pts = self.hLine.points
            pts[2] = x1
    
            # recreate the hLine
            self.hLine = Line(points=pts, width=2, cap='none')
    
            # clear the canvas
            self.wdgt1.canvas.clear()
            self.wdgt1.remove_widget(self.lbl)
    
            # redraw the canvas
            self.wdgt1.canvas.add(self.clr)
            self.wdgt1.canvas.add(self.hLine)
            self.wdgt1.add_widget(self.lbl)
    
    
    if __name__ == '__main__':
        Window.clearcolor = (1, 1, 1, 1)
        MyPaintApp().run()
    

    【讨论】:

      猜你喜欢
      • 2018-02-10
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多