如何将外部JSON文件的内容加载并显示到我的pythonkivy应用程序中

zqdjd7g9  于 2023-05-19  发布在  Python
关注(0)|答案(1)|浏览(115)

我正在尝试创建一个kivy应用程序,在第一个屏幕中,用户可以使用FileChooserIconView选择一个json配置文件。有一个空标签,它将通过将其内容转换为Python字典来显示JSON文件的内容。下面是我的方法,但我总是得到FileChooserIconViewer的on_submit属性的错误。
我得到的错误是在self.load_json_file上,它说:

Type "(file_chooser: Unknown, selection: Unknown) -> None" cannot be assigned to type "(selected: Unknown, touch: Unknown | None = None) -> None"
    Parameter name mismatch: "selected" versus "file_chooser"
    Parameter name mismatch: "touch" versus "selection"
    Parameter "selection" is missing default

这是我的main.py

from kivy.app import App 
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import json
from kivy.uix.filechooser import FileChooserListView, FileChooser, FileChooserIconView
from plyer import filechooser 
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.popup import Popup


class TestWindow(Screen):
    def selected(self, filename):
        try:
            with open("data.json") as f:
                data = json.load(f)
                self.json_text = str(data)
        except FileNotFoundError:
            self.json_text = "Error: File not found"
    def show_file_chooser(self):
        file_chooser = FileChooserIconView()
        file_chooser.filters = ["*.json"]
        file_chooser.path = '.'
        file_chooser.on_submit = self.load_json_file  
        popup = Popup(title= 'Select config json', content = file_chooser, size_hint=(0.8, 0.8))
        popup.open()
        
    def load_json_file(self, file_chooser, selection):
        if selection: 
            file_path = selection[0]
            with open(file_path, 'r') as file:
                json_dict = json.load(file)
                self.ids.json_label.text = str(json_dict) 

class WelcomeWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass

kv = Builder.load_file("Cluster.kv")

class ClusterApp(App):
    def build(self):
        # Window.clearcolor = (1, 1, 1, 1)
        return kv
if __name__ == "__main__":
    ClusterApp().run()

这是我的集群.kv:

WindowManager:
    TestWindow
    WelcomeWindow
    

<TestWindow>
    name: "test"

    GridLayout:
        cols: 1
        Button: 
            text: "select json config"
            on_release: root.show_file_chooser()
        Label:
            id: json_content
            text: ""

<WelcomeWindow>:
    name: "welcome"

    BoxLayout :
        orientation : "vertical"
        spacing: dp(50)
        size_hint: 0.5, 0.5
        pos_hint: {"center_x" : 0.5, "center_y" : 0.5}

        Button:
            text: "start clustering"

        Button: 
            text : "Settings"
            on_release: 
                app.root.current = "settings"
                root.manager.transition.direction = "right"

请有人提出一个方法来做到这一点。我是kivy的新手,网上没有足够的资源来讨论如何在kivy中处理json文件。一些关于同样的资源也会很棒。多谢了!

ws51t4hk

ws51t4hk1#

load_json_file()方法的方法签名不正确。尝试更改:

def load_json_file(self, file_chooser, selection):

致:

def load_json_file(self, selection, touch):

相关问题