178 lines
6.3 KiB
Python
178 lines
6.3 KiB
Python
from kivy.app import App
|
|
from kivy.uix.boxlayout import BoxLayout
|
|
from kivy.uix.button import Button
|
|
from kivy.uix.label import Label
|
|
from kivy.uix.popup import Popup
|
|
from kivy.core.window import Window
|
|
from kivy.utils import platform
|
|
from kivy.core.text import LabelBase
|
|
import os
|
|
# 根据平台设置不同的文件选择器
|
|
if platform == 'android':
|
|
from androidstorage4kivy import SharedStorage, Chooser
|
|
else:
|
|
from kivy.uix.filechooser import FileChooserListView
|
|
|
|
class FileUploadApp(App):
|
|
def build(self):
|
|
# 尝试注册中文字体
|
|
LabelBase.register('Roboto', './font/wqy-zenghei.ttc')
|
|
self.title_font = 'Roboto'
|
|
self.label_font = 'Roboto'
|
|
# 然后在创建所有Label和Button时指定字体
|
|
title_label = Label(
|
|
text="文件上传工具",
|
|
font_name=self.title_font,
|
|
font_size='24sp'
|
|
)
|
|
# 设置窗口背景色为浅灰色
|
|
Window.clearcolor = (0.95, 0.95, 0.95, 1)
|
|
|
|
# 主布局 - 垂直排列
|
|
main_layout = BoxLayout(orientation='vertical', spacing=20, padding=40)
|
|
|
|
# 标题
|
|
title_label = Label(
|
|
text="文件上传工具",
|
|
font_size='24sp',
|
|
size_hint=(1, 0.2),
|
|
color=(0, 0, 0, 1)
|
|
)
|
|
|
|
# 文件上传按钮布局 - 水平排列
|
|
file_buttons_layout = BoxLayout(orientation='horizontal', spacing=20, size_hint=(1, 0.3))
|
|
|
|
# 第一个文件上传按钮
|
|
self.file1_btn = Button(
|
|
text="上传文件1",
|
|
background_color=(0.26, 0.53, 0.96, 1),
|
|
size_hint=(0.5, 1),
|
|
on_press=self.choose_file1
|
|
)
|
|
|
|
# 第二个文件上传按钮
|
|
self.file2_btn = Button(
|
|
text="上传文件2",
|
|
background_color=(0.26, 0.53, 0.96, 1),
|
|
size_hint=(0.5, 1),
|
|
on_press=self.choose_file2
|
|
)
|
|
|
|
file_buttons_layout.add_widget(self.file1_btn)
|
|
file_buttons_layout.add_widget(self.file2_btn)
|
|
|
|
# 文件信息显示标签
|
|
self.file1_label = Label(
|
|
text="未选择文件1",
|
|
size_hint=(1, 0.1),
|
|
halign='left',
|
|
color=(0.3, 0.3, 0.3, 1)
|
|
)
|
|
|
|
self.file2_label = Label(
|
|
text="未选择文件2",
|
|
size_hint=(1, 0.1),
|
|
halign='left',
|
|
color=(0.3, 0.3, 0.3, 1)
|
|
)
|
|
|
|
# 生成按钮
|
|
generate_btn = Button(
|
|
text="开始生成",
|
|
background_color=(0.2, 0.8, 0.2, 1),
|
|
size_hint=(1, 0.2),
|
|
on_press=self.generate_files
|
|
)
|
|
|
|
# 将所有部件添加到主布局
|
|
main_layout.add_widget(title_label)
|
|
main_layout.add_widget(file_buttons_layout)
|
|
main_layout.add_widget(self.file1_label)
|
|
main_layout.add_widget(self.file2_label)
|
|
main_layout.add_widget(Label(size_hint=(1, 0.1))) # 空白间隔
|
|
main_layout.add_widget(generate_btn)
|
|
|
|
# 初始化文件路径
|
|
self.file1_path = None
|
|
self.file2_path = None
|
|
|
|
return main_layout
|
|
|
|
def choose_file1(self, instance):
|
|
"""选择第一个文件"""
|
|
if platform == 'android':
|
|
self.chooser = Chooser(self.chooser_callback1)
|
|
self.chooser.choose_content("file/*")
|
|
else:
|
|
self.show_file_chooser(1)
|
|
|
|
def choose_file2(self, instance):
|
|
"""选择第二个文件"""
|
|
if platform == 'android':
|
|
self.chooser = Chooser(self.chooser_callback2)
|
|
self.chooser.choose_content("file/*")
|
|
else:
|
|
self.show_file_chooser(2)
|
|
|
|
def chooser_callback1(self, shared_file_list):
|
|
"""Android上选择文件1的回调"""
|
|
if shared_file_list:
|
|
shared_file = shared_file_list[0]
|
|
self.file1_path = shared_file
|
|
self.file1_label.text = f"文件1: {shared_file}"
|
|
|
|
def chooser_callback2(self, shared_file_list):
|
|
"""Android上选择文件2的回调"""
|
|
if shared_file_list:
|
|
shared_file = shared_file_list[0]
|
|
self.file2_path = shared_file
|
|
self.file2_label.text = f"文件2: {shared_file}"
|
|
|
|
def show_file_chooser(self, file_num):
|
|
"""在非Android平台上显示文件选择器"""
|
|
content = BoxLayout(orientation='vertical')
|
|
file_chooser = FileChooserListView()
|
|
select_btn = Button(text='选择', size_hint_y=None, height=50)
|
|
|
|
popup = Popup(title=f"选择文件{file_num}", content=content, size_hint=(0.9, 0.9))
|
|
|
|
def select_file(instance):
|
|
if file_chooser.selection:
|
|
file_path = file_chooser.selection[0]
|
|
if file_num == 1:
|
|
self.file1_path = file_path
|
|
self.file1_label.text = f"文件1: {file_path}"
|
|
else:
|
|
self.file2_path = file_path
|
|
self.file2_label.text = f"文件2: {file_path}"
|
|
popup.dismiss()
|
|
|
|
select_btn.bind(on_press=select_file)
|
|
|
|
content.add_widget(file_chooser)
|
|
content.add_widget(select_btn)
|
|
popup.open()
|
|
|
|
def generate_files(self, instance):
|
|
"""开始生成按钮的回调函数"""
|
|
if not self.file1_path and not self.file2_path:
|
|
self.show_message("错误", "请至少选择一个文件")
|
|
elif not self.file1_path:
|
|
self.show_message("提示", f"正在处理文件2: {self.file2_path}")
|
|
elif not self.file2_path:
|
|
self.show_message("提示", f"正在处理文件1: {self.file1_path}")
|
|
else:
|
|
self.show_message("成功", f"正在处理文件1和文件2\n{self.file1_path}\n{self.file2_path}")
|
|
|
|
def show_message(self, title, message):
|
|
"""显示消息弹窗"""
|
|
content = BoxLayout(orientation='vertical', spacing=10, padding=10)
|
|
content.add_widget(Label(text=message))
|
|
close_btn = Button(text="关闭", size_hint_y=None, height=50)
|
|
popup = Popup(title=title, content=content, size_hint=(0.8, 0.5))
|
|
close_btn.bind(on_press=popup.dismiss)
|
|
content.add_widget(close_btn)
|
|
popup.open()
|
|
|
|
if __name__ == '__main__':
|
|
FileUploadApp().run() |