How to Create Mobile Apps
using python
Lecturer: Kuo
Date: 3/28

OUTLINE
- What's Kivy?
- How to use Kivy
What is Kivy?
- GUI
- 支援多平台(windows, linux, mac, andriod, ios)
How to use Kivy?
$python -m pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew
$python -m pip install kivy.deps.gstreamer
$python -m pip install kivy
windows download
How to use Kivy?
三個特殊關鍵字:
分入口函數(main.py)和介面風格(hello.kv)
- app:總是引用你的應用程序的實例
- root:引用當前規則中的根部件/模板
- self:引用當前部件
Example
from kivy.app import App
from kivy.uix.button import Button
class TutorialApp(App):
def build(self):
return Button(text='click me!', background_color=(0, 0, 1, 1), font_size=100)
TutorialApp().run()
Example

Example

import kivy
from kivy.app import App
class helloApp(App):
pass
window = helloApp()
window.run()
hello.py
Example
FloatLayout:
canvas.before:
Color:
rgba:0, 139/255.0, 0, 1
Rectangle:
pos: self.pos
size: self.size
Button:
text: 'Hello World'
size_hint: .5, .5
pos_hint:{'center_x':.5, 'center_y':.5}
hello.kv
Example

Example
from kivy.app import App
from kivy.lang import Builder
root = Builder.load_string(
'''
FloatLayout:
canvas.before:
Color:
rgba:0, 1, 0, 1
Rectangle:
pos: self.pos
size: self.size
Button:
text: 'Hello World'
size_hint: .5, .5
pos_hint:{'center_x':.5, 'center_y':.5}
'''
)
class MainApp(App):
def build(self):
return root
MainApp().run()
寫在一起
Example
from kivy.app import App
from kivy.graphics import Color, Rectangle
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import AsyncImage
class RootWidget(BoxLayout):
pass
class CustomLayout(FloatLayout):
def __init__(self, **kwargs):
super(CustomLayout, self).__init__(**kwargs)
with self.canvas.before:
Color(0, 139/255.0,0, 1)
self.rect = Rectangle(size=self.size, pos=self.pos)
self.bind(size=self._update_rect, pos=self._update_rect)
def _update_rect(self, instance, value):
self.rect.pos = instance.pos
self.rect.size = instance.size
class MainApp(App):
def build(self):
root = RootWidget()
c = CustomLayout()
root.add_widget(c)
c.add_widget(
AsyncImage(
source="https://dogmemes.com/wp-content/uploads/2020/03/tumblr_onyvh1wbss1vi3bo0o1_500-255x270.jpg",
size_hint= (1, .5),
pos_hint={'center_x':.5, 'center_y':.5}))
root.add_widget(AsyncImage(source='https://i.barkpost.com/wp-content/uploads/2015/02/featmeme.jpg?q=70&fit=crop&crop=entropy&w=808&h=500'))
c = CustomLayout()
c.add_widget(
AsyncImage(
source="https://www.rd.com/wp-content/uploads/2019/04/01-Hilarious-Dog-Memes.jpg",
size_hint= (1, .5),
pos_hint={'center_x':.5, 'center_y':.5}))
root.add_widget(c)
return root
MainApp().run()
用python寫
Example

END
How to Create Mobile Apps With Python
By penguin123
How to Create Mobile Apps With Python
- 193