Reputation: 31
As seen above picture, I want to make my apples remaining on spot, and replicated apples become draagable object. Which way will it be possible?
Here's my code now.
mn.py
from kivy.uix.widget import Widget
from kivy.uix.behaviors import DragBehavior
from kivy.app import App
from kivy.properties import StringProperty
class apple_0(DragBehavior,Widget):
def on_touch_move(self,touch):
return super(apple_0, self).on_touch_move(touch)
class apple_1(DragBehavior,Widget):
def on_touch_move(self,touch):
return super(apple_1, self).on_touch_move(touch)
class base_0(Widget):
def basepicture_0(self):
pass
class mn(Widget):
pass
class mnApp(App):
def build(self):
return mn()
if __name__=='__main__':
mnApp().run()
mn.kv
<mn>:
canvas:
Color:
rgb: 1,1,1,1
Rectangle:
size: root.width,root.height
pos: 0,0
Label:
pos: 30, root.top- 70
text:
('[size=20][color=000000]'
'fnc_slide_0'
'[/color][/0ize]')
markup: True
base_0:
id: base_0_id
pos: root.width/7, root.height/3
apple_0:
id: apple_0_id
pos: root.width/1.7,root.height/1.6
auto_bring_to_front: True
apple_1:
id: apple_1_id
pos: root.width/1.5,root.height/1.6
auto_bring_to_front: True
<base_0>
size: 944, 502
canvas:
Rectangle:
pos: self.pos
size: self.size
source: 'base_0.png'
<apple_0>:
size: 56.25,56.25
drag_rectangle: self.x, self.y, self.width, self.height
drag_timeout: 10000000
drag_distance: 0
canvas:
Rectangle:
pos: self.pos
size: self.size
source: 'apple_0.png'
<apple_1>:
size: 28.125,56.25
drag_rectangle: self.x, self.y, self.width, self.height
drag_timeout: 10000000
drag_distance: 0
canvas:
Rectangle:
pos: self.pos
size: self.size
source: 'apple_1.png'
Upvotes: 1
Views: 47
Reputation: 12189
Create a new widget that will be clickable and could store an image. Use canvas
if you want, but I did it with Image
, then you can just pass source
in __init__
. Such a widget will have a function that will accept position and a class of a widget you want to spawn.
When the spawner is clicked, it creates a new instance of a widget (therefore the class in arguments) and sets its position to position of the spawner. As the widget that's on the top catches all the touches, no further on_release
will be triggered unless you move the widget or click in the place that's not covered by your apple (the edges if your apple image is round).
Only edited parts of:
.py
class Spawner(ButtonBehavior, Image):
def spawn(self, pos, widgetcls, *args):
widget_instance = widgetcls(pos=pos)
self.parent.add_widget(widget_instance)
.kv
#:import Factory kivy.factory.Factory
<mn>:
canvas:
Color:
rgb: 1,1,1,1
Rectangle:
size: root.width,root.height
pos: 0,0
Label:
pos: 30, root.top- 70
text:
('[size=20][color=000000]'
'fnc_slide_0'
'[/color][/0ize]')
markup: True
base_0:
id: base_0_id
pos: root.width/7, root.height/3
Spawner:
id: apple_0_id
pos: root.width/1.7,root.height/1.6
on_release: self.spawn(self.pos, Factory.apple_0)
Spawner:
id: apple_1_id
pos: root.width/1.5,root.height/1.6
on_release: self.spawn(self.pos, Factory.apple_1)
Or do it directly in kv
, with self.parent.add_widget(Factory.<widget>(pos=self.pos))
, but that's less flexible.
Upvotes: 1