【发布时间】:2013-12-26 00:20:21
【问题描述】:
我正在制作一个会像大表盘一样旋转的圆圈。目前,我在顶部有一个箭头来显示表盘的朝向。我希望它的行为有点像老式的旋转电话,这样当你的手指/光标向下时你可以旋转它,但在你放手后它会(慢慢地)拉回顶部。
这是我的对象的样子:
这是我的代码:
#!/usr/bin/kivy
import kivy
kivy.require('1.7.2')
import math
from random import random
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import Color, Ellipse, Rectangle
class MinimalApp(App):
title = 'My App'
def build(self):
root = RootLayout()
return(root)
class RootLayout(AnchorLayout):
pass
class Circley(RelativeLayout):
angle = 0
def on_touch_down(self, touch):
ud = touch.ud
ud['group'] = g = str(touch.uid)
return True
def on_touch_move(self, touch):
ud = touch.ud
# print(touch.x, 0)
# print(self.center)
# print(0, touch.y)
# print(touch.x - self.center[0], touch.y - self.center[1])
y = (touch.y - self.center[1])
x = (touch.x - self.center[0])
calc = math.degrees(math.atan2(y,x))
angle = calc if calc > 0 else (360 + calc)
print(angle)
def on_touch_up(self, touch):
touch.ungrab(self)
ud = touch.ud
return True
if __name__ == '__main__':
MinimalApp().run()
还有kv:
#:kivy 1.7.2
#:import kivy kivy
<RootLayout>:
anchor_x: 'center' # I think this /is/ centered
anchor_y: 'center'
canvas.before:
Color:
rgba: 0.4, 0.4, 0.4, 1
Rectangle:
pos: self.pos
size: self.size
Circley:
anchor_x: 'center' # this is /not/ centered.
anchor_y: 'center'
canvas.before:
PushMatrix
Color:
rgba: 0.94, 0.94, 0.94, 1
Rotate:
angle: self.angle
axis: 0, 0, 1
origin: self.center
Ellipse:
source: 'arrow.png'
size: min(self.size), min(self.size)
pos: 0.5*self.size[0] - 0.5*min(self.size), 0.5*self.size[1] - 0.5*min(self.size)
Label:
text: unicode(self.size) # this is /not/ appearing
color: 1,0,0,1
canvas.after:
PopMatrix
其中的部分内容来自 kivy touchtracer 演示和from this SO question。
您可以看到我的计算正确地打印了圆的原点和触摸事件之间的角度(不确定这将如何响应多个手指,还没有考虑那么远),但不确定如何将其集成到界面中的“旋转”反馈事件中。
【问题讨论】:
标签: python layout user-interface kivy