K i v y Kivy Kivy

Kivy - Open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps.

Kivy runs on Linux, Windows, OS X, Android, iOS, and Raspberry Pi. You can run the same code on all supported platforms.


官网:https://kivy.org/#home


官方文档


Windows 下安装 kivy

pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
pip install kivy.deps.gstreamer -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
pip install kivy -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
pip install kivy_examples -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

验证 kivy 安装

from kivy.app import App
from kivy.uix.button import Button

class TestApp(App):
    def build(self):
        return Button(text='Major')
        
TestApp().run()

Kivy_python

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color,Ellipse,Line
from random import random
from kivy.uix.button import Button

class MyWidgetWidget(Widget):
    def on_touch_down(self, touch):
        color=(random(),random(),random())
        with self.canvas:
            Color(*color)
            touch.ud['Line']=Line(points=(touch.x,touch.y),width=5)

    def on_touch_move(self, touch):
        touch.ud['Line'].points=touch.ud['Line'].points+[touch.x,touch.y]

class MyPaintApp(App):
    def build(self):
        parent=Widget()
        self.painter=MyWidgetWidget()
        clearbtn=Button(text="Clear")
        clearbtn.bind(on_release=self.clear_canvas)
        parent.add_widget(self.painter)
        parent.add_widget(clearbtn)
        return parent

    def clear_canvas(self,obj):
        self.painter.canvas.clear()

if __name__=="__main__":
    MyPaintApp().run()

Kivy_linux_02

打包成apk安卓应用