https://cycleuser.gitbooks.io/kivy-guide-chinese/content/02-Kivy-Basics.html

https://www.e-learn.cn/content/wangluowenzhang/186622


https://kivy.org/doc/stable/gettingstarted/installation.html

Kivy 是一套用于跨平台快速应用开发的开源框架,只需编写一套代码,便可运行于各大桌面及移动平台上(包括 Linux, Windows, OS X, Android, iOS, 以及 Raspberry Pi) Kivy 采用 Python 和 Cython 编写,在国外已经十分火爆,受关注程度甚至一度超越了老牌的 Python GUI 工具 PyQt。可惜 Kivy 在国内还鲜为人知,咪博士将会陆续推出一系列 Kivy 中文教程。这一篇先教大家,在 Windows 上 安装 Kivy。

零、准备工作

请确保你的电脑上,已经安装了 Python 3.6, 并且已经设置好了 pip 国内源(国内镜像)。

具体可以参考咪博士此前的 2 篇教程


  1. Python 3.6.3 官网 下载 安装 测试 入门教程 (windows)

  2. Python pip 下载速度慢? Windows 设置 国内源,用 阿里云 国内镜像 加速
    一、Windows 下安装 kivy

  3. 启动 Windows 命令行窗口
    按 Windows 徽标 + R,输入 cmd,再按回车启动 Windows 命令行窗口

  4. pip 安装 kivy 依赖
    在 windows 命令行中,执行以下命令

1 python -m pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew

2 python -m pip install kivy.deps.gstreamer

  1. 安装 kivy
    在 windows 命令行中,执行以下命令

python -m pip install kivy

  1. 安装 kivy 官方示例
    在 windows 命令行中,执行以下命令

1 python -m pip install kivy_examples

二、验证 kivy 安装

在 Python IDLE 中,依次输入下面的代码(注意缩进)

1 from kivy.app import App

2 from kivy.uix.button import Button

3

4 class TestApp(App):

5 def build(self):

6 return Button(text=‘iPaoMi’)

7

8 TestApp().run()

最后,你将看到执行上面的 Python 代码,会运行如下的窗口,这可以算是 kivy 版的 hello world 了。

通过 kivy 提供的打包工具,你可以将 kivy 程序打包,运行到不同的平台上,包括各大主流的桌面系统和手机上(如 Android, iOS)。关于这些话题,请关注咪博士后续的 kivy 中文教程。

【坚信技术技术改变世界】

【欢迎学习交流】

服务器应用程序

#install_twisted_rector must be called before importing and using the reactor
from kivy.support import install_twisted_reactor

install_twisted_reactor()

from twisted.internet import reactor
from twisted.internet import protocol


class EchoServer(protocol.Protocol):
def dataReceived(self, data):
response = self.factory.app.handle_message(data)
if response:
self.transport.write(response)


class EchoServerFactory(protocol.Factory):
protocol = EchoServer

def __init__(self, app):
self.app = app


from kivy.app import App
from kivy.uix.label import Label


class TwistedServerApp(App):
label = None

def build(self):
self.label = Label(text="server started\n")
reactor.listenTCP(8000, EchoServerFactory(self))
return self.label

def handle_message(self, msg):
msg = msg.decode('utf-8')
self.label.text = "received: {}\n".format(msg)

if msg == "ping":
msg = "Pong"
if msg == "plop":
msg = "Kivy Rocks!!!"
self.label.text += "responded: {}\n".format(msg)
return msg.encode('utf-8')


if __name__ == '__main__':
TwistedServerApp().run()





***#客户端应用程序***

# install_twisted_rector must be called before importing the reactor
from __future__ import unicode_literals

from kivy.support import install_twisted_reactor

install_twisted_reactor()

# A Simple Client that send messages to the Echo Server
from twisted.internet import reactor, protocol


class EchoClient(protocol.Protocol):
def connectionMade(self):
self.factory.app.on_connection(self.transport)

def dataReceived(self, data):
self.factory.app.print_message(data.decode('utf-8'))


class EchoClientFactory(protocol.ClientFactory):
protocol = EchoClient

def __init__(self, app):
self.app = app

def startedConnecting(self, connector):
self.app.print_message('Started to connect.')

def clientConnectionLost(self, connector, reason):
self.app.print_message('Lost connection.')

def clientConnectionFailed(self, connector, reason):
self.app.print_message('Connection failed.')


from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout


# A simple kivy App, with a textbox to enter messages, and
# a large label to display all the messages received from
# the server
class TwistedClientApp(App):
connection = None
textbox = None
label = None

def build(self):
root = self.setup_gui()
self.connect_to_server()
return root

def setup_gui(self):
self.textbox = TextInput(size_hint_y=.1, multiline=False)
self.textbox.bind(on_text_validate=self.send_message)
self.label = Label(text='connecting...\n')
layout = BoxLayout(orientation='vertical')
layout.add_widget(self.label)
layout.add_widget(self.textbox)
return layout

def connect_to_server(self):
reactor.connectTCP('localhost', 8000, EchoClientFactory(self))

def on_connection(self, connection):
self.print_message("Connected successfully!")
self.connection = connection

def send_message(self, *args):
msg = self.textbox.text
if msg and self.connection:
self.connection.write(msg.encode('utf-8'))
self.textbox.text = ""

def print_message(self, msg):
self.label.text += "{}\n".format(msg)


if __name__ == '__main__':
TwistedClientApp().run()

打包教程网址

https://kivy.org/doc/stable/guide/packaging.html

Windows安装教程


新的kivy教程网址

http://www.doc88.com/p-9072562377588.html

kivy文件操作

https://cloud.tencent.com/developer/ask/154961

kivy打包成安卓

kivy速成班代码

https://github.com/inclement/kivycrashcourse/blob/master/video2-building_an_android_apk/tutorial2_resources.txt

安卓打包虚拟机

https://www.jianshu.com/p/a3f7f57e97ba

buildozer安装打包环境教程

https://txzone.net/files/torrents/kivy-buildozer-vm-2.0.zip

新教程

kivy笔记