Python Kivy-拖动时使对象被复制
如上图所示,我想让我的苹果留在原地,复制的苹果变成可拖动的对象。 哪条路可能 这是我的密码 mn.pyPython Kivy-拖动时使对象被复制,python,kivy,Python,Kivy,如上图所示,我想让我的苹果留在原地,复制的苹果变成可拖动的对象。 哪条路可能 这是我的密码 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_to
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()
class Spawner(ButtonBehavior, Image):
def spawn(self, pos, widgetcls, *args):
widget_instance = widgetcls(pos=pos)
self.parent.add_widget(widget_instance)
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'
#: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)
:
画布:
颜色:
rgb:1,1,1,1
矩形:
尺寸:根。宽度,根。高度
位置:0,0
标签:
位置:30,根。顶部-70
正文:
(“[size=20][color=000000]”
“fnc_幻灯片_0”
“[/color][/0ize]”
标记:正确
基本0:
id:base\u 0\u id
位置:根宽/7,根高/3
苹果0:
id:apple\u 0\u id
位置:根部宽度/1.7,根部高度/1.6
自动\u将\u带到\u前端:真
苹果1:
id:apple_1_id
位置:根部宽度/1.5,根部高度/1.6
自动\u将\u带到\u前端:真
尺码:944502
画布:
矩形:
pos:self.pos
大小:self.size
来源:'base_0.png'
:
尺寸:56.25,56.25
拖动矩形:self.x、self.y、self.width、self.height
拖动超时:10000000
拖动距离:0
画布:
矩形:
pos:self.pos
大小:self.size
资料来源:“apple_0.png”
:
尺寸:28.125,56.25
拖动矩形:self.x、self.y、self.width、self.height
拖动超时:10000000
拖动距离:0
画布:
矩形:
pos:self.pos
大小:self.size
资料来源:“apple_1.png”
创建一个可单击并可存储图像的新小部件。如果您愿意,可以使用canvas
,但我使用Image
,然后您可以在\uuuuu init\uuuu
中传递源代码。这样的小部件将具有一个函数,该函数将接受您想要生成的小部件的位置和类
单击产卵器时,它会创建一个小部件的新实例(因此是参数中的类),并将其位置设置为产卵器的位置。由于位于顶部的小部件捕捉到所有触摸,除非您移动小部件或单击苹果未覆盖的位置(如果您的苹果图像为圆形,则边缘为圆形),否则不会触发发布时的进一步
仅编辑以下内容的部分:
.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()
class Spawner(ButtonBehavior, Image):
def spawn(self, pos, widgetcls, *args):
widget_instance = widgetcls(pos=pos)
self.parent.add_widget(widget_instance)
.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'
#: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)
#:导入工厂kivy.Factory.Factory
:
画布:
颜色:
rgb:1,1,1,1
矩形:
尺寸:根。宽度,根。高度
位置:0,0
标签:
位置:30,根。顶部-70
正文:
(“[size=20][color=000000]”
“fnc_幻灯片_0”
“[/color][/0ize]”
标记:正确
基本0:
id:base\u 0\u id
位置:根宽/7,根高/3
产卵器:
id:apple\u 0\u id
位置:根部宽度/1.7,根部高度/1.6
发布时:self.spawn(self.pos,Factory.apple\u 0)
产卵器:
id:apple_1_id
位置:根部宽度/1.5,根部高度/1.6
发布时:self.spawn(self.pos,Factory.apple\u 1)
或者直接在kv
中使用self.parent.add_小部件(Factory.(pos=self.pos))
进行操作,但灵活性较低