Kivy | 跨平臺軟件開發 - 图1
© dotmodus

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

Install

  1. sudo add-apt-repository ppa:kivy-team/kivy-daily
  2. sudo apt-get update
  3. sudo apt-get install python-kivy
  1. sudo python3.7 -m pip install --upgrade pip wheel setuptools bcm
  2. sudo python3.7 -m pip install docutils pygments
  3. sudo python3.7 -m pip install kivy.deps.gstreamer --extra-index-url https://kivy.org/downloads/packages/simple/

Quick Start

Kivy | 跨平臺軟件開發 - 图2

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

  1. ## Creat a Hello.kv file in the path you run your script
  2. vim hello.kv
  3. ### this is for Hello.kv
  4. BoxLayout:
  5. Label:
  6. text: "Hello"
  1. ##!/usr/bin/python3.6
  2. from kivy.app import App
  3. class HelloApp(App):
  4. pass
  5. if __name__ == '__main__':
  6. HelloApp().run()

You need to name the kv file as hello.kv.
This file is target by the function HelloApp in main.py automatically.

Text layout

  1. ## hello.kv file
  2. BoxLayout:
  3. Label:
  4. text: "Hello"
  5. Label:
  6. text: "Beautiful"
  7. Label:
  8. text: "World"

Kivy | 跨平臺軟件開發 - 图3

Button Layout

  1. ## hello.kv file
  2. AddLocationForm:
  3. <AddLocationForm@BoxLayout>:
  4. orientation: "vertical"
  5. BoxLayout:
  6. TextInput:
  7. Button:
  8. text: "Search"
  9. size_hint_x: 1
  10. size_hint_y: 0.3
  11. Button:
  12. text: "Current Location"
  13. size_hint_x: 0.5

Kivy | 跨平臺軟件開發 - 图4

  1. AddLocationForm:
  2. <AddLocationForm@BoxLayout>:
  3. orientation: "vertical"
  4. BoxLayout:
  5. height: "40dp"
  6. size_hint_y: None
  7. TextInput:
  8. size_hint_x: 50
  9. Button:
  10. text: "Search"
  11. size_hint_x: 25
  12. Button:
  13. text: "Current Location"
  14. size_hint_x: 25
  15. BoxLayout:
  16. Label:
  17. text: "Palo Alto, MX\nPalo Alto, US"

Kivy | 跨平臺軟件開發 - 图5
Kivy | 跨平臺軟件開發 - 图6

Events

Responding to Input

In this section, We’d like to acquire the input information from the input box.
For respond the Input message, two things we need to done:

  1. Assign an ID for TextInput
  2. line 5: Acquiring Input.
  3. line 10: Assign the ID “search_box” to TextInput Box
  4. line 15: active function search_location()
  5. Assign an function for responding the text
  6. line 5: import the function
  7. line 11-12: assigning the function to acquire and respond the massage from TextInput

Weather.kv:

  1. AddLocationForm:
  2. <AddLocationForm@BoxLayout>:
  3. orientation: "vertical"
  4. search_input: search_box # Change one
  5. BoxLayout:
  6. height: "40dp"
  7. size_hint_y: None
  8. TextInput:
  9. id: search_box # Assign an ID
  10. size_hint_x: 50
  11. Button:
  12. text: "Search"
  13. size_hint_x: 25
  14. on_press: root.search_location()
  15. Button:
  16. text: "Current Location"
  17. size_hint_x: 25
  18. BoxLayout:
  19. Label:
  20. text: "Palo Alto, MX\nPalo Alto, US"

main.py:

  1. ##!/usr/local/bin/python3.7
  2. from kivy.app import App
  3. from kivy.uix.boxlayout import BoxLayout
  4. from kivy.properties import ObjectProperty
  5. class AddLocationForm(BoxLayout):
  6. search_input = ObjectProperty()
  7. def search_location(self):
  8. print("The user searched for '{}'".format(self.search_input.text))
  9. ##
  10. ##
  11. class WeatherApp(App):
  12. pass
  13. if __name__ == '__main__':
  14. WeatherApp().run()

Image left: GUI Layout; Image right: result was printed in terminal after clicked the button search.

Kivy | 跨平臺軟件開發 - 图7
Kivy | 跨平臺軟件開發 - 图8
In the current version Kivy1.11.1,
The function ListView is not is not supported.
Use Recycle As instead.

Take this as example:
from: https://stackoverflow.com/questions/56601384/kivy-unknown-class-listview-error-code
main.py

Light Weather App

  1. from kivy.app import App
  2. from kivy.uix.boxlayout import BoxLayout
  3. from kivy.uix.recycleview.views import RecycleDataViewBehavior
  4. from kivy.uix.label import Label
  5. from kivy.properties import BooleanProperty, ObjectProperty
  6. from kivy.uix.recycleboxlayout import RecycleBoxLayout
  7. from kivy.uix.behaviors import FocusBehavior
  8. from kivy.uix.recycleview.layout import LayoutSelectionBehavior
  9. from kivy.network.urlrequest import UrlRequest
  10. from kivy.lang import Builder
  11. import json
  12. class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
  13. RecycleBoxLayout):
  14. ''' Adds selection and focus behaviour to the view. '''
  15. class SelectableLabel(RecycleDataViewBehavior, Label):
  16. ''' Add selection support to the Label '''
  17. index = None
  18. selected = BooleanProperty(False)
  19. selectable = BooleanProperty(True)
  20. def refresh_view_attrs(self, rv, index, data):
  21. ''' Catch and handle the view changes '''
  22. self.index = index
  23. return super(SelectableLabel, self).refresh_view_attrs(
  24. rv, index, data)
  25. def on_touch_down(self, touch):
  26. ''' Add selection on touch down '''
  27. if super(SelectableLabel, self).on_touch_down(touch):
  28. return True
  29. if self.collide_point(*touch.pos) and self.selectable:
  30. return self.parent.select_with_touch(self.index, touch)
  31. def apply_selection(self, rv, index, is_selected):
  32. ''' Respond to the selection of items in the view. '''
  33. self.selected = is_selected
  34. class AddLocationForm(BoxLayout):
  35. search_input = ObjectProperty()
  36. search_results = ObjectProperty()
  37. def search_location(self):
  38. search_template = "https://samples.openweathermap.org/data/2.5/find?q={}&appid=b6907d289e10d714a6e88b30761fae22"
  39. # search_template = "https://api.openweathermap.org/data/2.5/find?q={}&typle=like&appid=xyz" # Replace 'xyz' with your API Key (APPID)
  40. search_url = search_template.format(self.search_input.text)
  41. request = UrlRequest(search_url, self.found_location)
  42. def found_location(self, request, data):
  43. data = json.loads(data.decode()) if not isinstance(data, dict) else data
  44. cities = ["{} ({})".format(d['name'], d['sys']['country']) for d in data['list']]
  45. self.search_results.data = [{'text': str(x)} for x in cities]
  46. print(f"self.search_results.data={self.search_results.data}")
  47. class WeatherRoot(BoxLayout):
  48. pass
  49. class TestApp(App):
  50. title = "Weather App"
  51. def build(self):
  52. return Builder.load_file("main.kv")
  53. if __name__ == '__main__':
  54. TestApp().run()

mian.kv

  1. WeatherRoot:
  2. <WeatherRoot>:
  3. AddLocationForm:
  4. <SelectableLabel>:
  5. # Draw a background to indicate selection
  6. canvas.before:
  7. Color:
  8. rgba: (1, 0, 0, 1) if self.selected else (.0, 0.9, .1, .3)
  9. Rectangle:
  10. pos: self.pos
  11. size: self.size
  12. Color:
  13. rgba: (0, 0.9, .1, .3)
  14. Rectangle:
  15. pos: self.pos
  16. size: self.size
  17. <AddLocationForm>:
  18. orientation: "vertical"
  19. search_input: search_input
  20. search_results: search_results_list
  21. BoxLayout:
  22. height: "40dp"
  23. size_hint_y:None
  24. TextInput:
  25. id: search_input
  26. size_hint_x: 50
  27. focus: True
  28. multiline: False
  29. hint_text: 'Your city name'
  30. on_text_validate: root.search_location()
  31. Button:
  32. text: "Search"
  33. size_hint_x: 25
  34. on_press: root.search_location()
  35. Button:
  36. text: "Current Location"
  37. size_hint_x: 25
  38. RecycleView:
  39. id: search_results_list
  40. viewclass: 'SelectableLabel'
  41. SelectableRecycleBoxLayout:
  42. default_size: None, dp(26)
  43. default_size_hint: 1, None
  44. size_hint_y: None
  45. height: self.minimum_height
  46. orientation: 'vertical'
  47. multiselect: True
  48. touch_multiselect: True

Examples

UrlRequest

Origin from:coder.work

  1. from kivy.app import App
  2. ##kivy.require("1.9.1")
  3. from kivy.uix.boxlayout import BoxLayout
  4. from kivy.properties import ObjectProperty
  5. from kivy.network.urlrequest import UrlRequest
  6. class MyWidget(BoxLayout):
  7. def __init__(self,**kwargs):
  8. super(MyWidget,self).__init__(**kwargs)
  9. search_url = "http://api.openweathermap.org/data/2.5/forecast/daily?APPID=ef4f6b76310abad083b96a45a6f547be&q=new%20york"
  10. print search_url
  11. self.request = UrlRequest(search_url, self.res)
  12. print self.request
  13. print "Result: before success", self.request.result,"\n"
  14. def res(self,*args):
  15. print "Result: after success", self.request.result
  16. class MyApp(App):
  17. def build(self):
  18. return MyWidget()
  19. if __name__ == '__main__':
  20. MyApp().run()

Some Awesome Blogs

Build a Mobile Application With the Kivy Python Framework Mike Driscoll Nov 04, 2019

Official Gallery


Enjoy~

本文由Python腳本GitHub/語雀自動更新

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗