Line between circle widgets

24 views
Skip to first unread message

Yunis Hüseynov

unread,
Nov 20, 2020, 10:32:19 AM11/20/20
to Kivy development
Need Help
How can I draw lines between these points that attaches them also during dragg movement via widget properties?


import os
os.environ['KIVY_GL_BACKEND'] = 'sdl2'
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.widget import Widget
from kivy.graphics import Rotate, Rectangle, Line, Ellipse, Color
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.core.window import Window
from random import randint


Builder.load_string("""
<MainScreen>:
BoxLayout:
orientation: 'vertical'
Button:
text: ''
background_color: 0,0,0,1
size_hint:1.,0.5

Button:
text: 'Moderate curve or straight canals'
background_color: 128,0,0,1
font_size: 40
size_hint:1.,0.7
on_press: root.manager.transition.direction = 'left'
on_press: root.manager.current = 'straight'

<StraightScreen>:
BoxLayout:
id: straightscreen
orientation: 'horizontal'

RootWidget:
# pos:200,2000
""")
class MainScreen(Screen):
pass
class LearnScreen(Screen):
pass
# class StraightScreen(Screen):
# pass
class StraightScreen(Screen):
pass

class CircleWidget1(Widget):
def __init__(self, **kwargs):
super(CircleWidget1, self).__init__(**kwargs)
self.size = (20, 20)
with self.canvas:
Color(0, 0, 1, 1)
self.circle = Ellipse(pos=self.pos, size=self.size)
circle2=self.add_widget(CircleWidget2(pos=(200,200), size=(2,2)))
circle3=self.add_widget(CircleWidget3(pos=(200,100), size=(2,2)))
self.bind(pos=self.redraw, size=self.redraw)

def redraw(self, *args):
self.circle.size = self.size
self.circle.pos = self.pos

def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
# if the touch collides with our widget, let's grab it
touch.grab(self)

# and accept the touch.
return True

return super(CircleWidget1, self).on_touch_down(touch)

def on_touch_up(self, touch):
# check if it's a grabbed touch event
if touch.grab_current is self:
# don't forget to ungrab ourself, or you might have side effects
touch.ungrab(self)

# and accept the last up
return True

return super(CircleWidget1, self).on_touch_up(touch)

def on_touch_move(self, touch):
# check if it's a grabbed touch event
if touch.grab_current is self:
self.pos = touch.pos

# and accept the last move
return True

return super(CircleWidget1, self).on_touch_move(touch)
class CircleWidget2(Widget):
def __init__(self, **kwargs):
super(CircleWidget2, self).__init__(**kwargs)
self.size = (20, 20)
with self.canvas:
Color(1,0, 0, 1)
self.circle = Ellipse(pos=self.pos, size=self.size)
self.bind(pos=self.redraw, size=self.redraw)

def redraw(self, *args):
self.circle.size = self.size
self.circle.pos = self.pos

def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
# if the touch collides with our widget, let's grab it
touch.grab(self)

# and accept the touch.
return True

return super(CircleWidget2, self).on_touch_down(touch)

def on_touch_up(self, touch):
# check if it's a grabbed touch event
if touch.grab_current is self:
# don't forget to ungrab ourself, or you might have side effects
touch.ungrab(self)

# and accept the last up
return True

return super(CircleWidget2, self).on_touch_up(touch)

def on_touch_move(self, touch):
# check if it's a grabbed touch event
if touch.grab_current is self:
self.pos = touch.pos

# and accept the last move
return True

return super(CircleWidget2, self).on_touch_move(touch)
class CircleWidget3(Widget):
def __init__(self, **kwargs):
super(CircleWidget3, self).__init__(**kwargs)
self.size = (20, 20)
with self.canvas:
Color(0,1,0,1)
self.circle = Ellipse(pos=self.pos, size=self.size)
self.bind(pos=self.redraw, size=self.redraw)

def redraw(self, *args):
self.circle.size = self.size
self.circle.pos = self.pos

def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
# if the touch collides with our widget, let's grab it
touch.grab(self)

# and accept the touch.
return True

return super(CircleWidget3, self).on_touch_down(touch)

def on_touch_up(self, touch):
# check if it's a grabbed touch event
if touch.grab_current is self:
# don't forget to ungrab ourself, or you might have side effects
touch.ungrab(self)

# and accept the last up
return True

return super(CircleWidget3, self).on_touch_up(touch)

def on_touch_move(self, touch):
# check if it's a grabbed touch event
if touch.grab_current is self:
self.pos = touch.pos

# and accept the last move
return True

return super(CircleWidget3, self).on_touch_move(touch)

class RootWidget(Widget):

def __init__(self, **kwargs):
super(RootWidget, self).__init__(**kwargs)
circle1=self.add_widget(CircleWidget1(pos=(200,300), size=(2,2)))

sm = ScreenManager()
sm.add_widget(StraightScreen(name='straight'))


class Yendo(App):


def build(self):
return sm

if __name__ == '__main__':
Yendo().run()
Screenshot from 2020-11-20 19-19-08.png

Moazzam Muhammad

unread,
Jan 14, 2021, 11:37:06 AM1/14/21
to Kivy development
Try to make this question observable OR make easy.
Reply all
Reply to author
Forward
0 new messages