kivy
Kivy 是一個開源的 Python 框架(2011年),用于快速開發(fā)應用,實現(xiàn)各種當前流行的用戶界面,比如多點 觸摸等等。 Kivy 可以運行于 Windows, Linux, MacOS, Android, iOS 等當前絕大部分主流桌面/移 動端操作系統(tǒng)。 Kivy 基于 Python,界面UI文件和程序文件相互分離的設計思路,設計簡潔優(yōu)雅,語法易學,適合新 人入門。 目前 Kivy 的官方文檔還算很完善。
第一個應用
main.py:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import (
NumericProperty, ReferenceListProperty, ObjectProperty
)
from kivy.vector import Vector
from kivy.clock import Clock
class PongPaddle(Widget):
score = NumericProperty(0)
def bounce_ball(self, ball):
if self.collide_widget(ball):
vx, vy = ball.velocity
offset = (ball.center_y - self.center_y) / (self.height / 2)
bounced = Vector(-1 * vx, vy)
vel = bounced * 1.1
ball.velocity = vel.x, vel.y + offset
class PongBall(Widget):
velocity_x = NumericProperty(0)
velocity_y = NumericProperty(0)
velocity = ReferenceListProperty(velocity_x, velocity_y)
def move(self):
self.pos = Vector(*self.velocity) + self.pos
class PongGame(Widget):
ball = ObjectProperty(None)
player1 = ObjectProperty(None)
player2 = ObjectProperty(None)
def serve_ball(self, vel=(4, 0)):
self.ball.center = self.center
self.ball.velocity = vel
def update(self, dt):
self.ball.move()
# bounce of paddles
self.player1.bounce_ball(self.ball)
self.player2.bounce_ball(self.ball)
# bounce ball off bottom or top
if (self.ball.y < self.y) or (self.ball.top > self.top):
self.ball.velocity_y *= -1
# went of to a side to score point?
if self.ball.x < self.x:
self.player2.score += 1
self.serve_ball(vel=(4, 0))
if self.ball.right > self.width:
self.player1.score += 1
self.serve_ball(vel=(-4, 0))
def on_touch_move(self, touch):
if touch.x < self.width / 3:
self.player1.center_y = touch.y
if touch.x > self.width - self.width / 3:
self.player2.center_y = touch.y
class PongApp(App):
def build(self):
game = PongGame()
game.serve_ball()
Clock.schedule_interval(game.update, 1.0 / 60.0)
return game
if __name__ == '__main__':
PongApp().run()
pong.kv:
#:kivy 1.0.9
<PongBall>:
size: 50, 50
canvas:
Ellipse:
pos: self.pos
size: self.size
<PongPaddle>:
size: 25, 200
canvas:
Rectangle:
pos: self.pos
size: self.size
<PongGame>:
ball: pong_ball
player1: player_left
player2: player_right
canvas:
Rectangle:
pos: self.center_x - 5, 0
size: 10, self.height
Label:
font_size: 70
center_x: root.width / 4
top: root.top - 50
text: str(root.player1.score)
Label:
font_size: 70
center_x: root.width * 3 / 4
top: root.top - 50
text: str(root.player2.score)
PongBall:
id: pong_ball
center: self.parent.center
PongPaddle:
id: player_left
x: root.x
center_y: root.center_y
PongPaddle:
id: player_right
x: root.width - self.width
center_y: root.center_y
打包apk文件
python+buildozer+kivy打包apk文件 - 簡書
GitHub - kivy/python-for-android: Turn your Python application into an Android APK
beeware
用Python編寫,無處不在運行。
BeeWare是一套工具和庫的集合,每個工具和庫都可以協(xié)同工作,幫助您編寫跨平臺本地GUI Python應用程序。它包括:
- Toga,一個跨平臺的小部件工具包;
- Briefcase,一個將Python項目打包為可分發(fā)的工件并可發(fā)送給最終用戶的工具;
- 庫(如Rubicon ObjC)用于訪問平臺本地庫;
- 預編譯的Python版本,可用于官方Python安裝程序不可用的平臺。
您可以單獨使用每個工具,也可以將它們全部用作一套工具。
完整的BeeWare套件還包括使用BeeWare自己的庫編寫的軟件開發(fā)工具和應用程序。
BeeWare套件可用于macOS、Windows、Linux(使用GTK)、Android和iOS等移動平臺,以及Web。我們的長期路線圖中還支持其他平臺(如機頂盒和手表)。
參考資料:
https://blog.csdn.net/ZNJIAYOUYA/article/details/126553693
Tutorial 1 - Your first app - BeeWare Tutorial文章來源:http://www.zghlxwxcb.cn/news/detail-462015.html
https://github.com/beeware/briefcase文章來源地址http://www.zghlxwxcb.cn/news/detail-462015.html
到了這里,關于使用Python開發(fā)Android軟件的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!