Rog*_*Dec 1 button orthogonal python-3.x kivy
下图是带有按钮的GridLayout 10 x 10.
我想在等距/正交2d视图中创建相同的网格.
这意味着每个按钮,而不是正方形,它可能像菱形,如下图所示:
我怎样才能做到这一点?
我不认为你可以在kivy UIX小部件上实际进行3D旋转,但你可以进行2D旋转和缩放.以下是App在build()方法中执行此操作的示例:
from kivy.app import App
from kivy.graphics.context_instructions import PushMatrix, Rotate, Scale, PopMatrix
from kivy.properties import BooleanProperty
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
import numpy as np
def matrixToNumpy(mat):
a = []
for i in range(4):
b = []
for j in range(4):
b.append(mat[i*4+j])
a.append(b)
npmat = np.mat(a)
return npmat
class MyButton(Button):
def on_touch_down(self, touch):
if not self.parent.touched:
self.parent.touched = True
if self.parent.mat is None:
scale = matrixToNumpy(self.parent.sca.matrix)
rotate = matrixToNumpy(self.parent.rot.matrix)
self.parent.mat = np.matmul(rotate, scale)
self.parent.inv_mat = self.parent.mat.I
npTouch = np.mat([touch.x, touch.y, 0, 1.0])
convTouch = np.matmul(npTouch, self.parent.inv_mat)
touch.x = convTouch[0,0]
touch.y = convTouch[0,1]
return super(MyButton, self).on_touch_down(touch)
def on_touch_up(self, touch):
self.parent.touched = False
return super(MyButton, self).on_touch_up(touch)
class MyGridLayout(GridLayout):
touched = BooleanProperty(False)
def __init__(self, **kwargs):
super(MyGridLayout, self).__init__(**kwargs)
self.mat = None
self.inv_mat = None
class MyApp(App):
def build(self):
layout = MyGridLayout(cols=10)
with layout.canvas.before:
PushMatrix()
layout.sca = Scale(1.0, 0.5, 1.0)
layout.rot = Rotate(angle=45, axis=(0,0,1), origin=(400,300,0))
with layout.canvas.after:
PopMatrix()
for i in range (1, 101):
layout.add_widget(MyButton(text=str(i)))
return layout
MyApp().run()
Run Code Online (Sandbox Code Playgroud)
我怀疑通过聪明地应用这两者kivy.graphics.context_instructions,你可以模拟你想要的东西.该PushMatrix()和PopMatrix()限制的影响Scale,并Rotate以刚MyGridLayout.您应该能够使用layout.sca和layout.rot引用调整这些值.
我注意到我原来的答案Buttons看起来不错,但不再有效.我添加了一堆代码来解决这个问题.所有numpy矩阵的东西只是让鼠标按下位置与相同的坐标MyGridLayout.遗憾的是,CanvasKivy事件不会自动考虑应用缩放和旋转,因此需要额外的代码.
| 归档时间: |
|
| 查看次数: |
129 次 |
| 最近记录: |