用python(kivy模块)开发安卓影视App——主页面开发(一)

2024-02-25 20:50

本文主要是介绍用python(kivy模块)开发安卓影视App——主页面开发(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

由于kivy在国内使用较少,在开发道路上困难重重,本人也花了不少精力来进行开发,希望大家能够从中得到学习与锻炼。

下面是我的成果图

 

首先是py文件

import requests
import re
from kivy.properties import StringProperty
from kivymd.app import MDApp
from kivymd.uix.button import MDRectangleFlatButton
from kivymd.uix.card import MDCard
from kivymd.uix import bottomsheet
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.stacklayout import MDStackLayout
from kivy.uix.screenmanager import Screen
from kivymd.font_definitions import theme_font_styles
from kivy.core.text import LabelBase
from kivymd.uix.screen import MDScreen
from kivy.core.window import Window
Window.size = (Window.height*3/4, Window.width*9/10)
class DealData():class_name = ['动漫', '追剧', '电影']class_key = ['4', '2', '1']class_url = 'https://saohuo.la/list/[].html'header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}class_list = '<ul class="v_list">(.*?)</ul></section><section class="page">'class_list_name = '<li><div class="v_img"><a href=".*?" title="(.*?)"><img class='class_list_picture = '<img class="lazyload" alt=".*?" data-original="(.*?)" /></a><div class='class_list_href = '<li><div class="v_img"><a href="(.*?)" title=".*?"><img class='class_list_text = '<div class="v_note">(.*?)</div></div><p class='search_url = 'https://saohuo.la/search.php?page=1&searchword=KEYWORDS&searchtype='search_list = '<ul class="v_list">(.*?)</ul></section><section class="page">'search_video_name = '<li><div class="v_img"><a href=".*?" title="(.*?)"><img class='search_video_url = '<li><div class="v_img"><a href="(.*?)" title=".*?"><img class='search_video_pic = '<img class="lazyload" data-original="(.*?)" /></a><div class="v_note">'search_video_data = '<div class="v_note">(.*?)</div>'video_class_data = '<ul class="from_list">([\s\S]*?)</ul>'video_class_name = '<li class=".*?">([\s\S]*?)</li>'video_play_data = '<section class="grid_box">([\s\S]*?)</section>'video_play_list = '<ul class="play_list"([\s\S]*?)</ul>'video_play_name = '<a title="[\s\S]*?" href="[\s\S]*?" target="_self">([\s\S]*?)</a>'video_play_url = '<a title="[\s\S]*?" href="([\s\S]*?)" target="_self">[\s\S]*?</a>'name_head = {'name': '骚火','href_head': 'https://saohuo.la', 'image_head': ''}# 首页影视分类列表
class MovieLayoutData(MDStackLayout):pass# 屏幕切换列表
class MainMovieLayout(MDStackLayout):pass# 首页分类布局
class TabCard(MDCard):screen = StringProperty()text = StringProperty()def on_press(self):# 切换影视分类self.parent.parent.parent.parent.parent.parent.parent.parent.ids.screen_manager.current = self.screen
# 影视详情单
class ElementCard(MDCard):url_all_datas = DealData()image = StringProperty()title_name = StringProperty()pictext = StringProperty()video_href = StringProperty()# 主屏幕
class MainScreen(MDScreen):url_all_datas = DealData()num = 0def __init__(self):self.one_screen()self.add_class_name()def one_screen(self, **kwargs):super(MainScreen, self).__init__(**kwargs)# 主页分类标签列表def add_class_name(self):for i in range(len(self.url_all_datas.class_name)):header = TabCard()header.text = self.url_all_datas.class_name[i]header.screen = f'screen{i}'self.ids.movie_class.add_widget(header)self.add_screen_details()self.num = self.num+1# 切换主页分类def add_screen_details(self):screen = Screen(name=f'screen{str(self.num)}')screen.add_widget(self.add_movie_list())self.ids.screen_manager.add_widget(screen)# 主页分类列表def add_movie_list(self):movie_layout = MovieLayoutData()datas = self.pa_shou_ye()for data in datas:video_list = ElementCard()video_list.title_name = data['title']video_list.video_href = data['list_url']video_list.image = data['image']video_list.pictext = data['pictext']movie_layout.ids.movie_list_data.add_widget(video_list)return movie_layout# 爬取主页分类详情def pa_shou_ye(self):all_datas = []url = self.url_all_datas.class_url.replace('[]', self.url_all_datas.class_key[self.num])response = requests.get(url, headers=self.url_all_datas.header, timeout=6)html = response.content.decode('utf-8')try:list_item = re.findall(self.url_all_datas.class_list, html)[0]title_lists = re.findall(self.url_all_datas.class_list_name, list_item)href_lists = re.findall(self.url_all_datas.class_list_href, list_item)src_lists = re.findall(self.url_all_datas.class_list_picture, list_item)pictext_lists = re.findall(self.url_all_datas.class_list_text, list_item)for i in range(len(title_lists)):data = dict()data['name'] = self.url_all_datas.name_head['name']data['href_head'] = self.url_all_datas.name_head['href_head']data['image_head'] = self.url_all_datas.name_head['image_head']data['title'] = title_lists[i]data['list_url'] = data['href_head']+href_lists[i]data['image'] = data['image_head']+src_lists[i]data['pictext'] = pictext_lists[i]all_datas.append(data)return all_datasexcept:return all_datasclass MainApp(MDApp):def build(self):self.theme_cls.theme_style = 'Light'self.theme_cls.primary_palette = 'Gray'LabelBase.register(name='Lishu', fn_regular='Lishu.ttf')theme_font_styles.append('Lishu')self.theme_cls.font_styles['Lishu'] = ['Lishu', 16, False, 0.15]return MainScreen()if __name__ == '__main__':MainApp().run()

然后是kv文件

<MainScreen>:MDBoxLayout:orientation: 'vertical'MDBoxLayout:size_hint_y:.08orientation: 'vertical'MDBoxLayout:padding:[0,0,0,0]spacing:dp(10)MDIconButton:icon: 'menu'on_release:root.on_release()MDLabel:MDTextFieldRound:id:search_keynormal_color: app.theme_cls.primary_lighthint_text:'搜索'font_name: './Lishu.ttf'size_hint_x:2pos_hint: {'x':.1,'y': .2}MDIconButton:icon: 'magnify'on_release:root.on_press()MDIconButton:icon:'download'             ScreenManager:id:home_screen_managerMDScreen:name:'home_class_screen'MDBoxLayout:orientation: 'vertical'MDBoxLayout:size_hint_y:.05ScrollView:MDBoxLayout:id:movie_classadaptive_width:Truerows:1padding:[10,2,10,2] spacing:dp(5)         ScreenManager:id:screen_managerMovieLayoutData:size_hint_y:Noneheight:Window.width*1/100adaptive_height:True screen_manager:screen_manager  MainMovieLayout:size_hint_y:Noneheight:Window.width*1/100adaptive_height:True screen_manager:home_screen_manager  <MovieLayoutData>:   size_hint_y:1ScrollView:MDStackLayout:id:movie_list_datapadding:[10,2,10,10] spacing: '5dp'adaptive_height:True 
<ElementCard>:orientation: 'vertical'size_hint_x: .5size_hint_y: Noneheight:Window.height*1/4padding: dp(10)radius:[25]MDBoxLayout:orientation: 'horizontal'size_hint_y: .9AsyncImage:source:root.image MDBoxLayout:orientation: 'vertical'size_hint_y: .9MDLabel:text: root.title_namefont_size: 15bold:Truefont_name: './Lishu.ttf'halign:'center'MDLabel:text:root.pictextfont_name: './Lishu.ttf'halign:'center'font_size: 15     <TabCard>:icon:'movie-roll'size_hint: None,1radius:dp(15)MDIcon:icon:root.iconMDLabel:text: root.textfont_name:'Lishu.ttf'<MainMovieLayout>:   size_hint_y:1ScrollView:MDStackLayout:id:movie_listpadding:[10,2,10,10] spacing: '5dp'adaptive_height:True 

如果有朋友想直接使用该代码,需要设置一个字体文件,放在代码同一目录下,且名字命为Lishu.ttf即可直接使用

这篇关于用python(kivy模块)开发安卓影视App——主页面开发(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/746670

相关文章

Python结合PyWebView库打造跨平台桌面应用

《Python结合PyWebView库打造跨平台桌面应用》随着Web技术的发展,将HTML/CSS/JavaScript与Python结合构建桌面应用成为可能,本文将系统讲解如何使用PyWebView... 目录一、技术原理与优势分析1.1 架构原理1.2 核心优势二、开发环境搭建2.1 安装依赖2.2 验

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

一文详解如何在Python中从字符串中提取部分内容

《一文详解如何在Python中从字符串中提取部分内容》:本文主要介绍如何在Python中从字符串中提取部分内容的相关资料,包括使用正则表达式、Pyparsing库、AST(抽象语法树)、字符串操作... 目录前言解决方案方法一:使用正则表达式方法二:使用 Pyparsing方法三:使用 AST方法四:使用字

Python列表去重的4种核心方法与实战指南详解

《Python列表去重的4种核心方法与实战指南详解》在Python开发中,处理列表数据时经常需要去除重复元素,本文将详细介绍4种最实用的列表去重方法,有需要的小伙伴可以根据自己的需要进行选择... 目录方法1:集合(set)去重法(最快速)方法2:顺序遍历法(保持顺序)方法3:副本删除法(原地修改)方法4:

Python运行中频繁出现Restart提示的解决办法

《Python运行中频繁出现Restart提示的解决办法》在编程的世界里,遇到各种奇怪的问题是家常便饭,但是,当你的Python程序在运行过程中频繁出现“Restart”提示时,这可能不仅仅是令人头疼... 目录问题描述代码示例无限循环递归调用内存泄漏解决方案1. 检查代码逻辑无限循环递归调用内存泄漏2.

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

python logging模块详解及其日志定时清理方式

《pythonlogging模块详解及其日志定时清理方式》:本文主要介绍pythonlogging模块详解及其日志定时清理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录python logging模块及日志定时清理1.创建logger对象2.logging.basicCo

Python如何自动生成环境依赖包requirements

《Python如何自动生成环境依赖包requirements》:本文主要介绍Python如何自动生成环境依赖包requirements问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录生成当前 python 环境 安装的所有依赖包1、命令2、常见问题只生成当前 项目 的所有依赖包1、