由于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 = '(.*?)'
class_list_name = '(.*?) '
search_video_name = ''
search_video_data = '(.*?)'
video_class_data = '([\s\S]*?)'
video_class_name = '([\s\S]*?)'
video_play_data = '([\s\S]*?) '
video_play_list = ''
video_play_name = '([\s\S]*?)'
video_play_url = '[\s\S]*?'
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 = 0
def __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_datas
except:
return all_datas
class 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文件
:
MDBoxLayout:
orientation: 'vertical'
MDBoxLayout:
size_hint_y:.08
orientation: 'vertical'
MDBoxLayout:
padding:[0,0,0,0]
spacing:dp(10)
MDIconButton:
icon: 'menu'
on_release:root.on_release()
MDLabel:
MDTextFieldRound:
id:search_key
normal_color: app.theme_cls.primary_light
hint_text:'搜索'
font_name: './Lishu.ttf'
size_hint_x:2
pos_hint: {'x':.1,'y': .2}
MDIconButton:
icon: 'magnify'
on_release:root.on_press()
MDIconButton:
icon:'download'
ScreenManager:
id:home_screen_manager
MDScreen:
name:'home_class_screen'
MDBoxLayout:
orientation: 'vertical'
MDBoxLayout:
size_hint_y:.05
ScrollView:
MDBoxLayout:
id:movie_class
adaptive_width:True
rows:1
padding:[10,2,10,2]
spacing:dp(5)
ScreenManager:
id:screen_manager
MovieLayoutData:
size_hint_y:None
height:Window.width*1/100
adaptive_height:True
screen_manager:screen_manager
MainMovieLayout:
size_hint_y:None
height:Window.width*1/100
adaptive_height:True
screen_manager:home_screen_manager
:
size_hint_y:1
ScrollView:
MDStackLayout:
id:movie_list_data
padding:[10,2,10,10]
spacing: '5dp'
adaptive_height:True
:
orientation: 'vertical'
size_hint_x: .5
size_hint_y: None
height:Window.height*1/4
padding: dp(10)
radius:[25]
MDBoxLayout:
orientation: 'horizontal'
size_hint_y: .9
AsyncImage:
source:root.image
MDBoxLayout:
orientation: 'vertical'
size_hint_y: .9
MDLabel:
text: root.title_name
font_size: 15
bold:True
font_name: './Lishu.ttf'
halign:'center'
MDLabel:
text:root.pictext
font_name: './Lishu.ttf'
halign:'center'
font_size: 15
:
icon:'movie-roll'
size_hint: None,1
radius:dp(15)
MDIcon:
icon:root.icon
MDLabel:
text: root.text
font_name:'Lishu.ttf'
:
size_hint_y:1
ScrollView:
MDStackLayout:
id:movie_list
padding:[10,2,10,10]
spacing: '5dp'
adaptive_height:True
如果有朋友想直接使用该代码,需要设置一个字体文件,放在代码同一目录下,且名字命为Lishu.ttf即可直接使用
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)