Skip to content

【Python GUI库】Kivy概览

1.简介

1.1.Kivy

Kivy: Cross-platform Python Framework for NUI Development.

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

官网:https://kivy.org/

1.2.Feature

  • Python UI库
  • 跨平台:Linux, Windows, OS X, Android, iOS, Raspbery Pi
    • 支持多种输入设备和协议:WM_Touch, WM_Pen, Mac OS X Trackpad and Magic Mouse, Mtdev, Linux Kernel HID, TUIO等。
  • MIT协议
  • GPU加速:图形引擎基于OpenGL ES 2;自带20多种Widget;

2.Demo

2.1.安装

shell
pip install kivy

2.2.Demo

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


class MyPaintWidget(Widget):

    def on_touch_down(self, touch):
        color = (random(), 1, 1)
        with self.canvas:
            Color(*color, mode='hsv')
            d = 30.
            Ellipse(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d))
            touch.ud['line'] = Line(points=(touch.x, touch.y))

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


class MyPaintApp(App):

    def build(self):
        parent = Widget()
        self.painter = MyPaintWidget()
        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()

参考:https://kivy.org/doc/stable/tutorials/firstwidget.html

运行

![image-20211208233335395](【Python GUI库】Kivy概览.assets/image-20211208233335395.png)

2.3.打包

  • Windows: PyInstanller

参考:https://kivy.org/doc/stable/guide/packaging-windows.html#packaging-windows-requirements

3.总结

  • 支持GPU加速,图形性能应该不错,可以考虑用来开发工具软件。