我在Cloudinary上有18个Json文件,这些文件本身是使用Kivy库中的JsonStore创建的。使用documentation,我只需单击一个按钮就可以下载这些文件(文本本身用西里尔字母书写),并在弹出窗口中显示它们,在Windows和Linux上一切正常,但是在Android智能手机上,当我单击按钮,什么都没有发生,尽管应该显示文本。我究竟做错了什么?
代码本身将在下面给出。
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp
from kivymd.theming import ThemableBehavior
from kivymd.uix.list import OneLineIconListItem, MDList
import webbrowser
import os
from kivy.storage.jsonstore import JsonStore
from difflib import get_close_matches
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
from kivy.network.urlrequest import UrlRequest
KV = '''
<ContentNavigationDrawer>:
orientation: "vertical"
padding: "8dp"
spacing: "8dp"
AnchorLayout:
anchor_x: "left"
size_hint_y: None
height: avatar.height
Image:
id: avatar
size_hint: None, None
size: "100dp", "100dp"
source: "icon2.png"
MDLabel:
text: "..."
font_style: "Button"
size_hint_y: None
height: self.texture_size[1]
MDLabel:
text: "..."
font_style: "Caption"
size_hint_y: None
height: self.texture_size[1]
Screen:
# Our button, by clicking on which the text in the label is displayed
MDIconButton:
id: image
icon: ""
pos_hint: {"center_x": 0.5, "center_y": 0.71}
width: root.width*0.99
height:root.height*0.38
on_release: app.load_json()
MDLabel:
id: info
text: ""
halign: 'center'
MDFloatingActionButton:
icon: "arrow-left-bold"
elevation_normal: 10
pos_hint: {"center_x": 0.1, "center_y": 0.05}
on_release: app.previous()
MDFloatingActionButton:
elevation_normal: 10
icon: "arrow-right-bold"
pos_hint: {"center_x": 0.9, "center_y": 0.05}
on_release: app.next()
MDLabel:
id: page
text: ''
text_color: 0, 0, 1, 1
halign: "center"
pos_hint: {"center_y": 0.05}
MDLabel:
id: name
text: ''
pos_hint: {"center_x": 0.51, "center_y": 0.47}
MDIcon:
id: review
halign: "center"
pos_hint: {"center_y": 0.3}
icon: "thumb-up-outline"
font_size: "100sp"
NavigationLayout:
ScreenManager:
Screen:
BoxLayout:
orientation: 'vertical'
MDToolbar:
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
Widget:
MDNavigationDrawer:
id: nav_drawer
swipe_edge_width: 200
ContentNavigationDrawer:
id: content_drawer
ScrollView:
DrawerList:
padding: "5dp"
spacing: "15dp"
MDTextField:
id: search
hint_text: "..."
helper_text: "..."
helper_text_mode: "on_focus"
max_text_length: 30
MDRoundFlatIconButton:
icon: "feature-search"
text: "Search"
width: dp(250)
on_release: app.search()
# Some Buttons............
# ---------Popup----------
<Popups>:
ScrollView:
# Here is our downloaded text
MDLabel:
id: rev
text: ''
size_hint: None, None
size: self.texture_size
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
padding_x: 10
text_size: (root.width, None)
# font_style:
MDFloatingActionButton:
id: close
icon: "cancel"
text: "Close"
# set size of the button
#size_hint: 0.2, 0.1
# set position of the button
pos_hint: {"x":0.95, "y": -0.035}
on_release: app.close()
'''
class ContentNavigationDrawer(BoxLayout):
pass
class DrawerList(ThemableBehavior, MDList):
pass
class Popups(FloatLayout):
def __init__(self, text):
super(Popups, self).__init__()
try:
self.ids.rev.text = str(text)
except:
pass
class TestNavigationDrawer(MDApp):
def build(self):
return Builder.load_string(KV)
def on_start(self):
self.im_folder = 'im'
self.image_arr = []
self.name_for_urls = []
self.bad_rev = ['....png']
self.page = 1
# Here I take the name of the pictures for display and future use
for i in os.listdir('im'):
self.image_arr.append(i)
self.name_for_urls.append(i.replace(' ', '_').replace('png', 'json'))
print(self.name_for_urls)
self.rev_dir = 'revDir'
# ---url (The text itself is written in Cyrillic) ---
self.url = 'https://res.cloudinary.com/http-f0433942-xsph-ru/raw/upload/v1589534191/files/folder/'
self.store = JsonStore('hello.json')
if os.path.exists('hello.json'):
pass
else:
# put some values
self.store.put('Theme', name='Light')
self.color = self.store.get('Theme')['name']
if self.color == 'Light':
self.theme_black = False
else:
self.theme_black = True
self.theme_cls.theme_style = self.color
self.st = ''
self.show_page_image_name()
self.rev_show()
def got_json(self, req, result):
self.st = ''
self.root.ids.info.text = 'Start'
for key, value in result['Review'].items():
# print('{}: {}'.format(key, value))
self.st += '{}: {}'.format(key, value)
print(req)
self.root.ids.info.text = 'Loaded'
self.show_popup()
def load_json(self):
UrlRequest(self.url + self.name_for_urls[self.page - 1], self.got_json)
def show_popup(self):
self.show = Popups(text=str(self.st))
self.popupWindow = Popup(title="...", content=self.show,
size_hint=(None, None), size=(self.root.width, self.root.height))
# open popup window
self.popupWindow.open()
def close(self):
print(1)
self.popupWindow.dismiss()
# Attach close button press with popup.dismiss action
def show_page_image_name(self):
self.root.ids.page.text = str(self.page)
self.root.ids.image.icon = os.path.join(self.im_folder, self.image_arr[self.page - 1])
self.root.ids.name.text = self.image_arr[self.page - 1].replace('.png', '')
def search(self):
self.get = self.root.ids.search.text
try:
if self.get.isdigit() == True:
if int(self.get) > len(self.image_arr):
self.root.ids.search.hint_text = 'Pages Exceeded'
else:
self.root.ids.search.hint_text = 'Search'
self.page = int(self.get)
self.rev_show()
self.show_page_image_name()
else:
try:
self.root.ids.search.hint_text = 'Search'
matches = get_close_matches(self.get, self.image_arr)
self.page = self.image_arr.index(matches[0]) + 1
print(self.page)
self.rev_show()
self.show_page_image_name()
except:
self.root.ids.search.hint_text = '...'
except:
pass
def rev_show(self):
if self.image_arr[self.page - 1] in self.bad_rev:
self.root.ids.review.icon = 'thumb-down-outline'
else:
self.root.ids.review.icon = 'thumb-up-outline'
def previous(self):
self.page -= 1
if self.page == 0:
self.page = len(self.image_arr)
self.rev_show()
self.show_page_image_name()
print('previous')
def next(self):
self.page += 1
if self.page > len(self.image_arr):
self.page = 1
self.rev_show()
self.show_page_image_name()
print('next')
def click(self):
print('click')
TestNavigationDrawer().run()