kivy编程相关 JSON格式
kivy中文文档
https://github.com/nkiiiiid/Kivy-CN
kivy打包环境虚拟机,kivy调试
https://github.com/nkiiiiid/kivy-apk
kivy打包环境搭建指南
https://github.com/nkiiiiid/kivydev-note
kivy打包exe
https://cooolr.notion.site/kivy-exe-78e563e1368643af9d616f55eaad455e
docker版buildozer
https://github.com/liyuanrui/buildozer-docker
百度云加速
https://github.com/iikira/BaiduPCS-Go/releases
webview打包方法
https://github.com/linuxrootok/kivy-webview
webview控件化
https://github.com/xiaoyaoking/kivy-android-webview
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple bs4
kivy全局中文支持最简单的解决方法
对于一个python coder来说,kivy是非常吸引注意的一个GUI库,它只需编写一套代码,便可运行于各种平台系统上(包括 Linux, Windows, OS X, Android, iOS, 以及 Raspberry Pi),Kivy 采用 Python 和 Cython 编写,在国外已经十分火爆,受关注程度甚至一度超越了老牌的 Python GUI 工具 PyQt。
鉴于此,我也进行了系统的学习与研究,以后可以利用python来开发移动app,这将可以把python人工智能方向的功能体现在移动平台上。可以方便我开拓移动APP领域的业务。
与网上所有接触kivy的猿类一样,遇到的kivy中文支持的问题,网上大多数方案为本地方案及单页面方案。经过了解原码,做了如下全局中文支持修改方案。
本方法为微软雅黑字体整套进行匹配修改,当然也可更换其他字体进行修改:
1, 下载字体文件包,并解压,下载地址:https://download.csdn.net/download/michaelxguo/20618394。
备注:已经下载放在百度网盘
2,找到本机或服务器python安装目录(..\Python39(此为你的python安装目录所在位置)\Lib\site-packages\kivy\data\fonts),打开fonts文件夹,先备份文件夹内字体文件,然后用下载解压的字体文件复制到fonts文件夹内。
放置目录:(F:\kivy\Lib\site-packages\kivy\data\fonts)
3,然后运行项目,则全局中文支持搞定。
kivy窗口界面元素坐标体系是从左下角开始的xy轴体系
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | class MainScreen(FloatLayout): def __init__( self , * * kwargs): super (MainScreen, self ).__init__( * * kwargs) # 设置窗体不全屏 Window.fullscreen = False # 指定初始化后的主窗体大小,但是程序运行后,还是动态地改变大小 Window.size = ( 480 , 800 ) # 将文本框添加到布局内 self .add_widget(Label(text = '连接状态' , font_size = 23 , size_hint = (. 2 , . 05 ), pos = ( 0 , 760 ))) self .connState = TextInput(multiline = True , font_size = 18 , size_hint = ( 1 , . 1 ), pos = ( 0 , 680 ), background_color = ( 0.8 , 0.5 , 0.3 , 1 )) self .add_widget( self .connState) self .add_widget(Label(text = '消息内容' , font_size = 23 , size_hint = (. 2 , . 05 ), pos = ( 0 , 640 ))) self .msgData = TextInput(multiline = True , font_size = 18 , size_hint = ( 1 , . 45 ), pos = ( 0 , 280 ), background_color = ( 0.1 , 0.8 , 0.3 , 1 )) self .add_widget( self .msgData) self .add_widget(Label(text = '发送消息' , font_size = 23 , size_hint = (. 2 , . 05 ), pos = ( 0 , 240 ))) self .msgSent = TextInput(multiline = True , font_size = 18 , size_hint = ( 1 , . 25 ), pos = ( 0 , 40 ), background_color = ( 0.1 , 0.5 , 0.7 , 1 )) self .add_widget( self .msgSent) # 将按钮添加到布局内 closeWindows = Button(text = '关闭软件' , font_size = 23 , size_hint = (. 333 , . 05 ), pos = ( 320 , 0 )) btnAddMsg = Button(text = '添加消息' , font_size = 23 , size_hint = (. 333 , . 05 ), pos = ( 160 , 0 )) btnSendMsg = Button(text = '确认发送' , font_size = 23 , size_hint = (. 333 , . 05 ), pos = ( 0 , 0 )) closeWindows.bind(on_press = self .on_eventcloseWindows) self .add_widget(closeWindows) btnAddMsg.bind(on_press = self .on_eventAddMsg) self .add_widget(btnAddMsg) btnSendMsg.bind(on_press = self .on_eventSendMsg) self .add_widget(btnSendMsg) # 定时刷新消息框 Clock.schedule_interval( self .refresh_schedule, 0.1 ) def refresh_schedule( self , dt = 0 ): # 写入connstate列表,列表元素为 string while params[ 'connstate' ]: self .msgData.text = self .msgData.text + "\r\n" + params[ 'connstate' ][ 0 ] del params[ 'connstate' ][ 0 ] def on_eventcloseWindows( self , obj): KivyApp().stop() |
JSON
格式
1 2 3 4 5 6 7 8 9 10 11 | { "RequestId" : "57b144cf-09fc-4916-a272-a62902d5b207" , "Success" : true , "Data" : { "DeviceName" : "device1" , "ProductKey" : "a1rYuVF****" , "DeviceSecret" : "tXHf4ezGEHcwdyMwoCDHGBmk9avi****" , "IotId" : "CqXL5h5ysRTA4NxjABjj0010fa****" , "Nickname" : "detectors_in_beijing" } } |
Python-flask同时启动两个端口服务,线程中启动flask
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | from flask import Flask from threading import Thread import os app1 = Flask( 'app1' ) @app1.route( '/' ) def foo(): return '1' Thread(target=lambda: app1.run(port=5001)).start() # ----------服务2----------------- app2 = Flask( 'app2' ) @app2.route( '/' ) def bar(): return 'hello world' app2.run(debug=True, port=5002) if __name__ == '__main__' : os.environ[ "WERKZEUG_RUN_MAIN" ] = 'true' Thread(target=app1).start() app2() |
Python线程中启动fastapi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import uvicorn from fastapi import FastAPI from pydantic import BaseModel from threading import Thread from tools import params, stdout import random myhttp = FastAPI() class Talk(BaseModel): id : str # 随机码 type : str # unlock/appConfig/deviceName/routine data: dict # 设置参数json @myhttp.post( "/setparam" ) async def setparam(item: Talk): print( '请求路由为------->setparam,内容:{}' . format (str(item)), stdout) params[item.data[ 'key' ]] = item.data[ 'value' ] # 获取 post 参数 return '参数:{},已经更新' . format (item.data[ 'key' ]) Thread(target=lambda: uvicorn.run(app=myhttp, host= '127.0.0.1' , port=8000)).start() class KivyApp(App): def build(self): return MainScreen() if __name__ == '__main__' : Thread(target=myhttp).start() threading.Thread(target=mqttconn).start() KivyApp().run() |
Python:操作dict时避免出现KeyError的几种方法
利用dict
内置的get(key[,default])
方法,如果key
存在,则返回其value
,否则返回default
;使用这个方法永远不会触发KeyError
,如:
1 2 3 4 5 6 7 8 9 10 | t = { 'a' : '1' , 'b' : '2' , 'c' : '3' , } print(t.get( 'd' , 'not exist' )) print(t) print(t.setdefault( 'd' , 'not exist' )) print(t) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix