#main.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics import Rectangle, Color
class BoxLayoutWidget(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# 设置背景
with self.canvas:
# 设置背景颜色,rgba格式,通常值为0-1之间(具体的值 / 255)
Color(1, 1, 1, 1)
self.rect = Rectangle(pos=self.pos, size=self.size)
self.bind(pos=self.update_rect, size=self.update_rect)
def update_rect(self, *args):
"""设置背尺寸"""
self.rect.pos = self.pos
self.rect.size = self.size
class BoxApp(App):
def build(self):
return BoxLayoutWidget()
if __name__ == '__main__':
BoxApp().run()
#box.kv
:
canvas:
Color:
rgba: [1, 1, 1, 1]
Rectangle:
size: self.size
pos: self.pos
#main.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class BoxLayoutWidget(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
class BoxApp(App):
def build(self):
return BoxLayoutWidget()
if __name__ == '__main__':
BoxApp().run()
#box.kv
:
canvas:
Color:
rgba: [1, 1, 1, 1]
Rectangle:
size: self.size
pos: self.pos
source: 'back.jpg'
#main.py
from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import Rectangle, Color
class RelativeWidget(RelativeLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# 设置背景
with self.canvas.before:
# 设置背景颜色,rgba格式,通常值为0-1之间(具体的值 / 255)
Color(1, 1, 1, 1)
self.rect = Rectangle(pos=self.pos, size=self.size)
self.bind(pos=self.update_rect, size=self.update_rect)
def update_rect(self, *args):
"""设置背尺寸"""
self.rect.pos = self.pos
self.rect.size = self.size
class RectangleApp(App):
def build(self):
return RelativeWidget()
if __name__ == '__main__':
RectangleApp().run()
#rectangle.kv
:
canvas:
Color:
rgba: [0, .5, .1, 1]
Rectangle:
size: self.width*0.2, self.height*0.15
pos: self.x+10,self.y+10
#main.py
from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import Rectangle, Color
class RelativeWidget(RelativeLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# 设置背景
with self.canvas.before:
# 设置背景颜色,rgba格式,通常值为0-1之间(具体的值 / 255)
Color(1, 1, 1, 1)
self.rect = Rectangle(pos=self.pos, size=self.size)
self.bind(pos=self.update_rect, size=self.update_rect)
def update_rect(self, *args):
"""设置背尺寸"""
self.rect.pos = self.pos
self.rect.size = self.size
class EllipseApp(App):
def build(self):
return RelativeWidget()
if __name__ == '__main__':
EllipseApp().run()
#ellipse.kv
:
canvas:
Color:
rgba: [.5, .1, .1, 1]
Ellipse:
size: self.width*0.3, self.height*0.25
pos: self.x+300,self.top-300
#main.py
from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import Rectangle, Color
class RelativeWidget(RelativeLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# 设置背景
with self.canvas.before:
# 设置背景颜色,rgba格式,通常值为0-1之间(具体的值 / 255)
Color(1, 1, 1, 1)
self.rect = Rectangle(pos=self.pos, size=self.size)
self.bind(pos=self.update_rect, size=self.update_rect)
def update_rect(self, *args):
"""设置背尺寸"""
self.rect.pos = self.pos
self.rect.size = self.size
class EllipseApp(App):
def build(self):
return RelativeWidget()
if __name__ == '__main__':
EllipseApp().run()
#ellipse.kv
:
canvas:
Color:
rgba: [.5, .1, .1, 1]
Ellipse:
size: self.width*0.3, self.height*0.25
pos: self.x+300,self.top-300
angle_start: 120
angle_end: 420
#main.py
from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import Rectangle, Color
class RelativeWidget(RelativeLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# 设置背景
with self.canvas.before:
# 设置背景颜色,rgba格式,通常值为0-1之间(具体的值 / 255)
Color(1, 1, 1, 1)
self.rect = Rectangle(pos=self.pos, size=self.size)
self.bind(pos=self.update_rect, size=self.update_rect)
def update_rect(self, *args):
"""设置背尺寸"""
self.rect.pos = self.pos
self.rect.size = self.size
class TriangleApp(App):
def build(self):
return RelativeWidget()
if __name__ == '__main__':
TriangleApp().run()
#ellipse.kv
:
canvas:
Color:
rgba: [.5, .1, .1, 1]
Ellipse:
size: self.width*0.3, self.height*0.25
pos: self.x+300,self.top-300
segments: 3
#triangle.kv
:
canvas:
Color:
rgba: [.1, .1, .5, 1]
Triangle:
points: 310,250, 640,280, 480,500
#main.py
from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import Rectangle, Color
class RelativeWidget(RelativeLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# 设置背景
with self.canvas.before:
# 设置背景颜色,rgba格式,通常值为0-1之间(具体的值 / 255)
Color(1, 1, 1, 1)
self.rect = Rectangle(pos=self.pos, size=self.size)
self.bind(pos=self.update_rect, size=self.update_rect)
def update_rect(self, *args):
"""设置背尺寸"""
self.rect.pos = self.pos
self.rect.size = self.size
class QuadApp(App):
def build(self):
return RelativeWidget()
if __name__ == '__main__':
QuadApp().run()
#quad.kv
:
canvas:
Color:
rgba: [.5, .1, .1, 1]
Quad:
points: 310,250, 640,280, 480,500, 380,520
#main.py
from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import Rectangle, Color
class RelativeWidget(RelativeLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# 设置背景
with self.canvas.before:
# 设置背景颜色,rgba格式,通常值为0-1之间(具体的值 / 255)
Color(1, 1, 1, 1)
self.rect = Rectangle(pos=self.pos, size=self.size)
self.bind(pos=self.update_rect, size=self.update_rect)
def update_rect(self, *args):
"""设置背尺寸"""
self.rect.pos = self.pos
self.rect.size = self.size
class LineApp(App):
def build(self):
return RelativeWidget()
if __name__ == '__main__':
LineApp().run()
#line.kv
:
canvas:
Color:
rgba: [.1, .5, .5, 1]
Line:
ellipse: 210,320, 80, 60, 120,420,180
width: 2
Line:
circle: 350,350, 40, 0,360,180
Line:
rectangle: 410,310, 80, 70
Line:
points: 510,310, 540,390, 590,320
close: True
#point.kv
:
canvas:
Color:
rgba: [.1, .5, .5, 1]
Line:
points: 310,350, 640,380
Point:
points: 300,200, 300,300
pointsize: 3
#main.py
from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import Rectangle, Color
from kivy.graphics.instructions import InstructionGroup
class RelativeWidget(RelativeLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
with self.canvas:
Color(0, 0, 1, 0.2)
Rectangle(pos=self.pos, size=(300, 300))
Color(0, 1, 0, 0.4)
Rectangle(pos=(300, 300), size=(300, 300))
class CanvasApp(App):
def build(self):
return RelativeWidget()
if __name__ == '__main__':
CanvasApp().run()
#canvas.kv
:
canvas:
Color:
rgba: [1, 1, 1, 1]
Rectangle:
pos: self.pos
size: self.size
#main.py
from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import Rectangle, Color
class RelativeWidget(RelativeLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
class CanvasApp(App):
def build(self):
return RelativeWidget()
if __name__ == '__main__':
CanvasApp().run()
#box.kv
:
canvas.before:
Color:
rgba: [1, 1, 1, 1]
Rectangle:
pos: self.pos
size: self.size
canvas:
Color:
rgba: [.5, .1, .1, 1]
Rectangle:
pos: self.pos
size: 300, 300
canvas.after:
Color:
rgba: [.1, .5, .1, 1]
Rectangle:
pos: 200, 200
size: 300, 300
Button:
text: 'Button 0'
pos: 100, 100
size_hint: None, None
size: 300, 300
canvas.after:
Color:
rgba: [.1, .1, .5, 1]
Rectangle:
pos: self.pos
size: 100, 100
#main.py
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
class RotateGridLayoutWidget(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
class RotateApp(App):
def build(self):
return RotateGridLayoutWidget()
if __name__ == "__main__":
RotateApp().run()
#rotate.kv
:
source: 'kivy-logo-black-64.png'
pos: self.parent.pos
size_hint: .5, .4
canvas:
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
:
cols: 2
canvas:
Color:
rgba: (1, 1, 1, 1)
Rectangle:
pos: self.pos
size: self.siz
Button:
text: "col:1, row:1"
FloatLayout:
MyImage:
Button:
text: "col:1, row:2"
#main.py
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
class RotateGridLayoutWidget(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
class RotateApp(App):
def build(self):
return RotateGridLayoutWidget()
if __name__ == "__main__":
RotateApp().run()
#rotate.kv
:
source: 'kivy-logo-black-64.png'
pos: self.parent.pos
size_hint: .5, .4
canvas:
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
PopMatrix
:
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"
#main.py
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
class RotateGridLayoutWidget(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
class RotateApp(App):
def build(self):
return RotateGridLayoutWidget()
if __name__ == "__main__":
RotateApp().run()
#rotate.kv
:
source: 'kivy-logo-black-64.png'
pos: self.parent.pos
size_hint: .5, .4
canvas.before:
PushMatrix
Rotate:
axis: (0,0,1)
angle: 60
origin: self.center
canvas:
Color:
rgba: 1,0,0,.5
Line:
rectangle: self.x, self.y, self.width, self.height
canvas.after:
PopMatrix
:
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"
#main.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class TranslateBoxLayoutWidget(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
class TranslateApp(App):
def build(self):
return TranslateBoxLayoutWidget()
if __name__ == "__main__":
TranslateApp().run()
#translate.kv
:
source: 'kivy-logo-black-64.png'
pos: self.parent.pos
size_hint: .5, .4
canvas.before:
PushMatrix
Translate:
x: -100
y: 200
z: 0
canvas:
Color:
rgba: 1,0,0,.5
Line:
rectangle: self.x, self.y, self.width, self.height
canvas.after:
PopMatrix
:
canvas:
Color:
rgba: (1, 1, 1, 1)
Rectangle:
pos: self.pos
size: self.size
Button:
text: "Button 0"
FloatLayout:
MyImage:
Button:
text: "Button 1"
#main.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class ScaleBoxLayoutWidget(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
class ScaleApp(App):
def build(self):
return ScaleBoxLayoutWidget()
if __name__ == "__main__":
ScaleApp().run()
#scale.kv
:
source: 'kivy-logo-black-64.png'
pos: self.parent.pos
size_hint: .5, .4
canvas.before:
PushMatrix
Scale:
xyz: (1.25, 1.25, 0)
canvas:
Color:
rgba: 1,0,0,.5
Line:
rectangle: self.x, self.y, self.width, self.height
canvas.after:
PopMatrix
:
canvas:
Color:
rgba: (1, 1, 1, 1)
Rectangle:
pos: self.pos
size: self.size
Button:
text: "Button 0"
FloatLayout:
MyImage:
Button:
text: "Button 1"
#main.py
from kivy.app import App
from kivy.graphics import Color, Line
from kivy.uix.behaviors import ToggleButtonBehavior
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.widget import Widget
from kivy.utils import get_color_from_hex
class FrameToggleButton(ToggleButton):
"""当前按钮添加边框"""
def do_press(self):
"""点击改变状态"""
if self.state == 'normal':
ToggleButtonBehavior.do_press(self)
class DrawCanvasWidget(Widget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# 设置默认颜色
self.change_color(get_color_from_hex('#19caad'))
self.line_width = 2
def on_touch_down(self, touch):
"""触摸显示轨迹"""
if Widget.on_touch_down(self, touch):
return
with self.canvas:
touch.ud['current_line'] = Line(points=(touch.x, touch.y), width=self.line_width)
def on_touch_move(self, touch):
"""连线"""
if 'current_line' in touch.ud:
touch.ud['current_line'].points += (touch.x, touch.y)
def change_color(self, new_color):
"""调色"""
self.last_color = new_color
self.canvas.add(Color(*new_color))
def change_line_width(self, line_width='Normal'):
"""线宽"""
self.line_width = {'Thin': 1, 'Normal': 2, 'Thick': 4}[line_width]
def clear_canvas(self):
"""清空"""
saved = self.children[:]
self.clear_widgets()
self.canvas.clear()
for widget in saved:
self.add_widget(widget)
self.change_color(self.last_color)
class PaintApp(App):
def build(self):
self.canvas_widget = DrawCanvasWidget()
return self.canvas_widget
if __name__ == '__main__':
PaintApp().run()
#paint.kv
#:import C kivy.utils.get_color_from_hex
:
group: 'color'
background_normal: 'radio_background_normal.png'
background_down: 'radio_background_down.png'
border: (3, 3, 3, 3)
on_release: app.canvas_widget.change_color(self.background_color)
:
group: 'line_width'
color: C('#2c3e50')
background_color: C('#ecf0f1')
background_normal: 'radio_background_normal.png'
background_down: 'radio_background_down.png'
border: (3, 3, 3, 3)
on_release: app.canvas_widget.change_line_width(self.text)
:
canvas.before:
Color:
rgba:[1, 1, 1, 1]
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
orientation: 'horizontal'
padding: 2
spacing: 2
x: 0
top: root.top
size_hint: None, None
size: 280, 44
LineWidthButton:
text: 'Thin'
LineWidthButton:
text: 'Normal'
state: 'down'
LineWidthButton:
text: 'Thick'
Button:
text: 'Clear'
on_release: root.clear_canvas()
BoxLayout:
id: bottom_box
orientation: 'horizontal'
padding: 2
spacing: 2
size: root.width, 40
BottomColorButton:
background_color: C('#19caad')
state: 'down'
BottomColorButton:
background_color: C('#8cc7b5')
BottomColorButton:
background_color: C('#a0eee1')
BottomColorButton:
background_color: C('#bee7e9')
BottomColorButton:
background_color: C('#beedc7')
BottomColorButton:
background_color: C('#d6d5b7')
BottomColorButton:
background_color: C('#d1ba74')
BottomColorButton:
background_color: C('#e6ceac')
BottomColorButton:
background_color: C('#ecad9e')
BottomColorButton:
background_color: C('#f4606c')
BottomColorButton:
background_color: C('#3498db')
BottomColorButton:
background_color: C('#1abc9c')
BottomColorButton:
background_color: C('#2ecc71')
BottomColorButton:
background_color: C('#f1c40f')
BottomColorButton:
background_color: C('#e67e22')
BottomColorButton:
background_color: C('#e74c3c')
BottomColorButton:
background_color: C('#9b59b6')
BottomColorButton:
background_color: C('#ecf0f1')
BottomColorButton:
background_color: C('#95a5a6')
BottomColorButton:
background_color: C('#000000')