好友
阅读权限40
听众
最后登录1970-1-1
|
漁滒
发表于 2020-9-25 13:26
本帖最后由 aiai 于 2020-9-25 13:29 编辑
python要开发app就要用到kivy的模块,关于kivy开发环境的安装网上非常多,就不多说了。
整个开发过程主要涉及布局和事件两个方面,首先是关于布局方面
[Python] 纯文本查看 复制代码 # 初始化部件
urlinput = TextInput(
text="",
size_hint=(1, 0.1),
pos_hint={'x': 0, 'y': 0.9},
hint_text="请输入链接地址"
)
cookieinput = TextInput(
text="",
size_hint=(1, 0.1),
pos_hint={'x': 0, 'y': 0.8},
hint_text="请输入cookie"
)
rundobutton = Button(
text="解析",
size_hint=(0.33, 0.08),
pos_hint={'x': 0, 'y': 0.72},
on_release=dorun
)
doemptyurlbutton = Button(
text="清空地址",
size_hint=(0.33, 0.08),
pos_hint={'x': 0.33, 'y': 0.72},
on_release=cleanurl
)
doemptycookiebutton = Button(
text="清空帐号",
size_hint=(0.34, 0.08),
pos_hint={'x': 0.66, 'y': 0.72},
on_release=cleancookie
)
nextdobutton = Button(
text="下一集",
size_hint=(0.33, 0.08),
pos_hint={'x': 0, 'y': 0.64},
on_release=donext
)
alldobutton = Button(
text="全集",
size_hint=(0.33, 0.08),
pos_hint={'x': 0.33, 'y': 0.64},
on_release=doall
)
copybutton = Button(
text="复制到剪切板",
size_hint=(0.34, 0.08),
pos_hint={'x': 0.66, 'y': 0.64},
on_release=copytext
)
resultinput = TextInput(
text="解析结果",
size_hint=(1, 0.64),
pos_hint={'x': 0, 'y': 0}
)
主要是设置每个部件的大小和位置,对于输入框设置文字和默认文字,对于按钮,设置点击事件的回调函数
设置好布局以后,就可以编写每个事件的逻辑了
[Python] 纯文本查看 复制代码 # 设置按钮部件的事件
def cleanurl(arg):
urlinput.text = ""
def cleancookie(arg):
cookieinput.text = ""
def dorun(arg):
# 设置响应的回调方法
def success(requests, results):
if results['code'] == '0':
resultinput.text = '解析结果\n'
for video in results['video_list']:
resultinput.text += results['video_title']+' '+str(video['vwidth'])+'X'+str(video['vheight'])+' '+video['definition']+' '+str(int(video['size']/1024/1024))+'MB '+video['codec_type']+'\n'
resultinput.text += base64.b64decode(video['main_url'].encode()).decode()+'\n'
else:
resultinput.text = '解析结果 '+results['message']
def error(requests, errors):
resultinput.text = '解析结果 '+str(errors)
# 请求解析结果
url = urlinput.text
headers = {'cookie': cookieinput.text.replace('\n', '').strip()}
apiurl = 'http://106.55.157.247:80/geturl/?url=' + url
UrlRequest(url=apiurl, req_headers=headers, on_success=success, on_error=error)
def donext(arg):
pass
def doall(arg):
pass
def copytext(arg):
resultinput.copy(data=resultinput.text)
在解析部分,因为涉及某些问题,所以将部分逻辑放到我自己的服务器上通过kivy提供的UrlRequest类来进行通信
同时需要为这个请求绑定成功和失败的两个回调函数
还有一个问题是因为kivy默认是不能显示中文的,所以还需要导入中文字体
[Python] 纯文本查看 复制代码 # 加载中文字体
resource_add_path(os.path.abspath('.'))
LabelBase.register('Roboto', 'DroidSansFallback.ttf')
最后就是构件一个app实例,将设置的部件添加进去,下面是完整代码
[Python] 纯文本查看 复制代码
import os
import base64
from kivy.app import App
from kivy.network.urlrequest import UrlRequest
from kivy.uix.floatlayout import FloatLayout
from kivy.resources import resource_add_path
from kivy.uix.textinput import TextInput
from kivy.core.text import LabelBase
from kivy.uix.button import Button
# 加载中文字体
resource_add_path(os.path.abspath('.'))
LabelBase.register('Roboto', 'DroidSansFallback.ttf')
# 设置按钮部件的事件
def cleanurl(arg):
urlinput.text = ""
def cleancookie(arg):
cookieinput.text = ""
def dorun(arg):
# 设置响应的回调方法
def success(requests, results):
if results['code'] == '0':
resultinput.text = '解析结果\n'
for video in results['video_list']:
resultinput.text += results['video_title']+' '+str(video['vwidth'])+'X'+str(video['vheight'])+' '+video['definition']+' '+str(int(video['size']/1024/1024))+'MB '+video['codec_type']+'\n'
resultinput.text += base64.b64decode(video['main_url'].encode()).decode()+'\n'
else:
resultinput.text = '解析结果 '+results['message']
def error(requests, errors):
resultinput.text = '解析结果 '+str(errors)
# 请求解析结果
url = urlinput.text
headers = {'cookie': cookieinput.text.replace('\n', '').strip()}
apiurl = 'http://106.55.157.247:80/geturl/?url=' + url
UrlRequest(url=apiurl, req_headers=headers, on_success=success, on_error=error)
def donext(arg):
pass
def doall(arg):
pass
def copytext(arg):
resultinput.copy(data=resultinput.text)
# 初始化部件
urlinput = TextInput(
text="",
size_hint=(1, 0.1),
pos_hint={'x': 0, 'y': 0.9},
hint_text="请输入链接地址"
)
cookieinput = TextInput(
text="",
size_hint=(1, 0.1),
pos_hint={'x': 0, 'y': 0.8},
hint_text="请输入cookie"
)
rundobutton = Button(
text="解析",
size_hint=(0.33, 0.08),
pos_hint={'x': 0, 'y': 0.72},
on_release=dorun
)
doemptyurlbutton = Button(
text="清空地址",
size_hint=(0.33, 0.08),
pos_hint={'x': 0.33, 'y': 0.72},
on_release=cleanurl
)
doemptycookiebutton = Button(
text="清空帐号",
size_hint=(0.34, 0.08),
pos_hint={'x': 0.66, 'y': 0.72},
on_release=cleancookie
)
nextdobutton = Button(
text="下一集",
size_hint=(0.33, 0.08),
pos_hint={'x': 0, 'y': 0.64},
on_release=donext
)
alldobutton = Button(
text="全集",
size_hint=(0.33, 0.08),
pos_hint={'x': 0.33, 'y': 0.64},
on_release=doall
)
copybutton = Button(
text="复制到剪切板",
size_hint=(0.34, 0.08),
pos_hint={'x': 0.66, 'y': 0.64},
on_release=copytext
)
resultinput = TextInput(
text="解析结果",
size_hint=(1, 0.64),
pos_hint={'x': 0, 'y': 0}
)
# 使用浮动布局
class IndexPage(FloatLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# 添加部件
self.add_widget(urlinput)
self.add_widget(cookieinput)
self.add_widget(rundobutton)
self.add_widget(doemptyurlbutton)
self.add_widget(doemptycookiebutton)
self.add_widget(nextdobutton)
self.add_widget(alldobutton)
self.add_widget(copybutton)
self.add_widget(resultinput)
class TestAPP(App):
# 重写build方法
def build(self):
# 返回一个布局实例
return IndexPage()
if __name__ == '__main__':
# 启动APP
TestAPP().run()
软件启动界面
解析成功界面
软件下载地址:https://www.lanzoux.com/b00zu3v5a
已知问题
1.下一集和全集按钮事件还没有编写逻辑,点击后暂无反应
2.cookie内容无法保存,退出软件后需要重新输入 |
免费评分
-
查看全部评分
|