我正在尝试将图像设置为屏幕的某个区域。
start_screen.py
import kivy
kivy.require('1.9.0')
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.clock import Clock
from functools import partial
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.animation import Animation
from kivy.core.window import Window
import time
def set_color(button, color, *args):
button.color = color
class ImageButton(ButtonBehavior, Image):
pass
class StartScreen(FloatLayout):
def __init__(self, **kwargs):
super(StartScreen, self).__init__(**kwargs)
def reset_color(*args):
Clock.schedule_once(partial(set_color,
self.ids.pstart_button,
(1,1,1,1)), 1)
Clock.schedule_once(partial(set_color,
self.ids.pstart_button,
(0,1,0,1)), 2)
Clock.schedule_interval(reset_color, 3)
#def on_touch_down(self, touch):
#if self.collide_point(*touch.pos):
#self.to_menu()
def to_menu(self):
sb=self.ids.pstart_button
anim = Animation(pos =(Window.width * .05, Window.height *.60))
anim &= Animation(size =(Window.width * .15, Window.height * .15))
anim.start(sb)
class TestApp(App):
def build(self):
return StartScreen()
if __name__=='__main__':
TestApp().run()
test.kv
#:kivy 1.9
<StartScreen>:
orientation: 'horizontal'
size: root.size
canvas.before:
Rectangle:
pos:self.pos
size: self.size
source: 'bg.png'
FloatLayout:
size: root.size
orientation: 'horizontal'
ImageButton:
id: pstart_button
size_hint: (.30,.30)
pos: root.center_x - (self.width/2), root.center_y - (self.height/2)
source: 'start_button.png'
on_touch_down: if pstart_button.collide_point(pstart_button.x, pstart_button.y): root.to_menu()
<MenuScreen>:
orientation: 'horizontal'
size: root.size
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'bg2.png'
FloatLayout:
size: root.size
orientation: 'horizontal'
ImageButton:
id: start_button
size_hint: ( .15, .15)
pos: root.width * .05, root.height *.60
source: 'start_button.png'
ImageButton:
id:usb_button
size_hint: (None, None)
width: start_button.width * .80
height: start_button.height * .80
x: start_button.width *.75
y: start_button.y - (start_button.height * .99)
source: r'usb_button.png'
ImageButton:
id:remote_button
size_hint: (None, None)
width: start_button.width * .80
height: start_button.height * .80
x: start_button.width *.75
y: usb_button.y - (start_button.height * .99)
source: r'remote_button.png'
ImageButton:
id:sdcard_button
size_hint: (None, None)
width: start_button.width * .80
height: start_button.height * .80
x: start_button.width *.75
y: remote_button.y - (start_button.height * .99)
source: r'sdcard_button.png'
ImageButton:
id:home_button
size_hint: ( .1,.1)
pos: root.width * .73, root.height *.88
source: 'home_button.png'
ImageButton:
id:settings_button
size_hint: ( .1, .1)
pos: home_button.x +(home_button.width * .89), root.height * .88
source: 'settings_button.png'
ImageButton:
id:back_button
size_hint: ( .1, .1)
pos: settings_button.x +(settings_button.width * .89), root.height * .88
source: 'back_button.png'
问题是,当我尝试动画并使用“自我”时,它最终动画我的背景以及小部件。我只想要动画小部件。我想我确定为什么自我移动整个事情因为自我是整个小部件布局。我的代码显示了我刚试图为小部件设置动画的地方,但它没有用。
此外,碰撞点似乎也不起作用。如果我点击屏幕上的任何地方,它会动画。我有一种感觉,如果我可以让图像移动,那么它也可以解决这个问题。
答案 0 :(得分:1)
我需要分开功能。我试图在主布局中为小部件设置动画。我应该做的是将小部件的功能(以及任何小部件)放在自己的类中。菜鸟错了。我将to_menu()移到了我的ImageButtton类,它运行起来了。当我将行为移动到按钮时,碰撞点也起作用。