Kivy安卓实战:7 |网页浏览器| 寫個工具箱把7 - 图1
© Karobben

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

  1. CryptoWatch-Kivy 1.13
  2. Kivy 2.0.0
  3. Kivy-Garden 0.1.4
  4. kivy-garden.wordcloud 1.0.0
  5. kivymd 0.104.2.dev0

Quick Review

  1. tree
  1. .
  2. ├── 123.fa
  3. ├── 123.html
  4. ├── Karobben_logo_horizontal_800.png
  5. ├── LICENSE
  6. ├── Layout
  7. ├── Blog.kv
  8. ├── CV_cm.kv
  9. ├── CV_test.kv
  10. ├── Data_table.kv
  11. ├── Font.kv
  12. ├── Navigation_Draw.kv
  13. ├── Navigation_Tabs.kv
  14. ├── Seq.kv
  15. ├── editor.kv
  16. ├── filechooser.kv
  17. └── menu.kv
  18. ├── README.md
  19. ├── alipay.jpg
  20. ├── buildozer.spec
  21. ├── config
  22. ├── Navi.json
  23. └── home.json
  24. ├── custom_camera
  25. ├── __init__.py
  26. ├── custom_camera.kv
  27. └── custom_camera.py
  28. ├── demo
  29. ├── clustal
  30. └── echart
  31. ├── favicon.ico
  32. ├── font
  33. ├── ArtificialBox-WdD4.ttf
  34. ├── FangZhengHeiTiFanTi-1.ttf
  35. ├── FangZhengHeiTiJianTi-1.ttf
  36. ├── FangZhengKaiTiPinYinZiKu-1.ttf
  37. ├── FangzhenXiaozhuan.ttf
  38. ├── HuaKangXinZhuanTi-1.ttf
  39. ├── JingDianFanJiaoZhuan-1.ttf
  40. ├── heydings-controls-1.ttf
  41. ├── heydings-icons-1.ttf
  42. ├── heydings-icons-2.ttf
  43. └── icon-works-webfont-2.ttf
  44. ├── image_processing
  45. ├── __init__.py
  46. ├── cascades
  47. └── haarcascade_frontalface_default.xml
  48. └── image_processing.py
  49. ├── libWidget
  50. ├── Blog.py
  51. ├── CV_cm.py
  52. ├── CV_test.py
  53. ├── Data_table.py
  54. ├── Font.py
  55. ├── Seq.py
  56. ├── editor.py
  57. ├── filechooser.py
  58. ├── menu.py
  59. └── model.txt
  60. ├── libs
  61. ├── bio_seq.py
  62. ├── clustalo.py
  63. ├── clustalo.pytxt
  64. ├── web_open.py
  65. └── webview.py
  66. ├── logo.png
  67. ├── main.py
  68. └── wepay.png

Function for Close Tab

Because I was using my blog project to doing the test with this server, so I called it as Blog.py

Function run_sever is for starting the http server so you can render CSS and applying js, close_blog could close the server and back to home directory.

  1. from kivy.lang import Builder
  2. from kivy.uix.popup import Popup
  3. from kivy.uix.floatlayout import FloatLayout
  4. from kivy.uix.screenmanager import Screen
  5. from kivy.properties import ObjectProperty
  6. from kivy.utils import platform
  7. import os
  8. import webbrowser, time
  9. import threading
  10. class FunctionWidget():
  11. def main(self):
  12. Builder.unload_file("Layout/Blog.kv")
  13. self.Function_page = Builder.load_file("Layout/Blog.kv")
  14. self.Function_page.ids.buton_start.on_release = self.open_blog
  15. self.Function_page.ids.buton_close.on_release = self.close_blog
  16. return self.Function_page
  17. def run_sever(self):
  18. import http.server
  19. import socketserver
  20. PORT = 5500
  21. Handler = http.server.SimpleHTTPRequestHandler
  22. with socketserver.TCPServer(("", PORT), Handler) as self.httpd:
  23. print("serving at port", PORT)
  24. self.httpd.serve_forever()
  25. def close_blog(self):
  26. PATH = os.path.abspath(__file__).split("libWidget")[0].replace("appc","app")
  27. print(PATH)
  28. try:
  29. self.httpd.shutdown()
  30. os.chdir(PATH)
  31. except:
  32. os.chdir(PATH)
  33. def open_blog(self, *args):
  34. if platform == "android":
  35. from libs.webview import WebView
  36. os.chdir("Blog")
  37. PATH = os.path.abspath(__file__).split("libWidget")[0].replace("appc","app")
  38. URL = 'file://'+PATH+'/demo/clustal/123.html',
  39. URL = 'http://127.0.0.1:5500/'
  40. print("URL = ", URL)
  41. x = threading.Thread(target=self.run_sever, args=())
  42. x.start()
  43. print("Started, test")
  44. if platform == "android":
  45. self.browser = None
  46. self.browser = WebView(URL,
  47. enable_javascript = True,
  48. enable_downloads = True,
  49. enable_zoom = True)
  50. else:
  51. try:
  52. webbrowser.open('http://127.0.0.1:5500/')
  53. except:
  54. pass

Layout

Blog.py

  1. MDBoxLayout:
  2. MDRectangleFlatButton:
  3. id: buton_start
  4. text: "Start Server"
  5. on_release: None
  6. pos_hint: {"center_x": .5, "center_y": .5}
  7. MDRectangleFlatButton:
  8. id: buton_close
  9. text: "Close Server"
  10. on_release: None
  11. pos_hint: {"center_x": .5, "center_y": .5}

Functions for webview

This function was written by RobertFlatt and published in RobertFlatt /Android-for-Python . He also contributed lots of other awesome functions and examples of widgets.

I copied his webview.py to libs directory. An example of using it:

  1. if platform == "android":
  2. from libs.webview import WebView
  3. self.browser = None
  4. self.browser = WebView(URL,
  5. enable_javascript = True,
  6. enable_downloads = True,
  7. enable_zoom = True)

Enjoy~

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

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

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