章节知识点总揽
上一篇代码中旋转了第二个子部件,但其中图像并未成功旋转(模块旋转,图像没有发生角度变化)。因为Rotate命令是图像在屏幕上已经确定为之后才执行的。
实例:设置图像旋转
可以通过canvas.before属性将Rotate指令的执行优先级提前,修改rotate.kv文件,具体代码如下:
<MyImage@Image>:
source:'01.jpg'
pos:self.parent.pos
size_hint:.5,.4
canvas.before:
PushMatrix
Rotate:
axis:(0,0,1)
angle:60
origin:self.center
Color:
rgba:1,0,0,.5
Line:
rectangle:self.x,self.y,self.width,self.height
canvas.after:
PopMatrix
<RotateGridLayoutWidget>:
cols:2
canvas:
Color:
rgba:(1,1,1,1)
Rectangle:
pos:self.pos
size:self.size
Button:
text:'col:1,row;1'
FloatLayout:
MyImage:
Button:
text:'col:1,row:2'
使用图片01.jpg,放置在main.py相同目录下:
使用上一篇的main.py文件,具体代码如下:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.graphics import Rectangle,Color
from kivy.graphics.instructions import InstructionGroup
class RotateGridLayoutWidget(GridLayout):
def __init__(self,**kwargs):
super().__init__(**kwargs)
class RotateApp(App):
def build(self):
return RotateGridLayoutWidget()
if __name__ == '__main__':
RotateApp().run()
执行main.py文件后,图像与模块同时发生了旋转。
上一篇:基本图形绘制——部分旋转
下一篇:基本图形绘制——平移坐标空间