如何在Kivy中使用等距/正交视图?

Rog*_*Dec 1 button orthogonal python-3.x kivy

下图是带有按钮的GridLayout 10 x 10.

https://i.stack.imgur.com/yZsJI.png

我想在等距/正交2d视图中创建相同的网格.

这意味着每个按钮,而不是正方形,它可能像菱形,如下图所示:

在此输入图像描述

我怎样才能做到这一点?

Joh*_*son 5

我不认为你可以在kivy UIX小部件上实际进行3D旋转,但你可以进行2D旋转和缩放.以下是Appbuild()方法中执行此操作的示例:

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.scalayout.rot引用调整这些值.

我注意到我原来的答案Buttons看起来不错,但不再有效.我添加了一堆代码来解决这个问题.所有numpy矩阵的东西只是让鼠标按下位置与相同的坐标MyGridLayout.遗憾的是,CanvasKivy事件不会自动考虑应用缩放和旋转,因此需要额外的代码.

这是它的样子: 这是生成的按钮数组

  • 要牢记几件事.首先,这只适用于包含`MyButton`实例的`MyGridLayout`(中间没有其他布局).其次,如果动态更改`Rotate`或`Scale`值,则需要在`MyGridLayout`实例中将`self.mat`设置为`None`以触发重新计算矩阵. (2认同)