求助:使用ScrollView与MDListItem无法显示JSON文件文本
问题修复方案
你的词典界面空白的问题源于多个代码逻辑错误和无效JSON格式,以下是具体修复步骤:
1. 修复无效JSON文件
你的JSON字符串包含未转义的换行符,这会导致json.load解析失败。将所有换行符删除或替换为\n,修正后的JSON如下:
{ "anopheles": "A genus of mosquitoes which are secondary hosts of the malaria parasites, and whose bite is the usual, if not the only, means of infecting human beings with malaria. Several species are found in the United States. They may be distinguished from the ordinary mosquitoes of the genus Culex by the long slender palpi, nearly equaling the beak in length, while those of the female Culex are very short. They also assume different positions when resting, Culex usually holding the body parallel to the surface on which it rests and keeping the head and beak bent at an angle, while Anopheles holds the body at an angle with the surface and the head and beak in line with it. Unless they become themselves infected by previously biting a subject affected with malaria, the insects cannot transmit the disease.", "uniclinal": "See Nonoclinal.", "sarong": "A sort of petticoat worn by both sexes in Java and the Malay Archipelago. Balfour (Cyc. of India)", "turcoman": "1. A member of a tribe of Turanians inhabiting a region east of the Caspian Sea. 2. A Turcoman carpet. Turcoman carpet or rug, a kind of carpet or rug supposed to be made by the Turcomans.", "corrugator": "A muscle which contracts the skin of the forehead into wrinkles.", "self-murder": "Suicide." }
2. 重构Python代码
移除错误的Dict类,改用直接填充MDList的方式展示数据:
from kivymd.app import MDApp from kivy.lang import Builder from kivymd.uix.floatlayout import MDFloatLayout from kivy.uix.floatlayout import FloatLayout from kivy.core.window import Window Window.clearcolor = (1, 1, 1, 1) Window.size = (350, 600) import datetime from kivy.metrics import dp from datetime import date from kivy.properties import StringProperty from kivymd.uix.behaviors import CommonElevationBehavior from kivy.uix.screenmanager import Screen, ScreenManager from kivymd.uix.screen import Screen import json from kivy.uix.boxlayout import BoxLayout from kivy.uix.label import Label from kivymd.uix.list import MDListItem # Screen for to-dos class Todos(CommonElevationBehavior, MDFloatLayout): title = StringProperty() description = StringProperty() class Notes(CommonElevationBehavior, MDFloatLayout): title = StringProperty() description = StringProperty() class Base(FloatLayout): pass class DoNote(MDApp): def build(self): global screen_manager screen_manager = ScreenManager() screen_manager.add_widget(Builder.load_file("kv_files\\layout.kv")) screen_manager.add_widget(Builder.load_file("kv_files\\dictionary.kv")) screen_manager.add_widget(Builder.load_file("kv_files\\todo_list.kv")) screen_manager.add_widget(Builder.load_file("kv_files\\add_todo.kv")) screen_manager.add_widget(Builder.load_file("kv_files\\note_screen.kv")) screen_manager.add_widget(Builder.load_file("kv_files\\add_note.kv")) return screen_manager def on_start(self): # 启动时填充词典数据 self.populate_dictionary() def populate_dictionary(self): try: # 获取词典界面的MDList容器 container = screen_manager.get_screen("dictionary_screen").ids.container container.clear_widgets() # 加载JSON数据(仅加载一次) with open('lol\\dictionary.json','r') as fh: dictionary = json.load(fh) # 为每个词典条目创建MDListItem for word in dictionary.keys(): item = MDListItem(text=word) item.text_color = (1, 0, 0, 1) # 设置红色文本 container.add_widget(item) except Exception as e: print(f"填充词典出错: {e}") if __name__ == "__main__": DoNote().run()
3. 简化词典界面KV文件
移除无用的<Dict>规则,保留核心布局:
#:import get_color_from_hex kivy.utils.get_color_from_hex MDScreen: name: 'dictionary_screen' MDIconButton: icon: "arrow-left" user_font_size: "40sp" pos_hint: {'center_y': .96} text_color: get_color_from_hex('#7f2f00ff') on_release: root.manager.transition.direction = "down" root.manager.current = 'main_screen' MDBoxLayout: orientation: "vertical" MDBoxLayout: padding: 30 orientation: "vertical" MDScrollView: MDList: id: container theme_text_color: "Custom" text_color: 1, 0, 0, 1
关键修复点说明
- 移除错误的RecycleView用法:原
Dict类试图在初始化时访问未存在的父容器,且错误地将RecycleView添加到MDList中。 - 优化JSON加载:仅加载一次JSON文件,避免循环中重复IO操作。
- 修复JSON格式:删除字符串中的换行符,确保JSON解析正常。
- 直接填充MDList:利用MDList与ScrollView的原生兼容性,直接添加列表项,逻辑更简单。
内容的提问来源于stack exchange,提问作者Yca Ciel
相关产品推荐
相关产品推荐

