Python 是否可以从App()中修改不同类中的变量,而不实例化类的新实例

Python 是否可以从App()中修改不同类中的变量,而不实例化类的新实例,python,python-3.x,kivy,kivy-language,Python,Python 3.x,Kivy,Kivy Language,我正在尝试使用kivy的设置面板。然而,我无法让它在不实例化我试图使用的类的新对象的情况下从应用程序中访问或修改类变量。这对我想做的事是不可接受的。json文件包含在一个名为MyJson.json的文件中,my_json是实际的数据。我知道这是可行的,因为设置面板会很好地弹出 我想使用一个设置面板,因为它的布局在这里或在一个类似的方式。但是,我需要在设置面板中更改这些值,以便在myScreen中的变量发生变化时进行更改 是否可以像我在下面的示例中所做的那样,在不创建MySScreen的新对象的情

我正在尝试使用kivy的设置面板。然而,我无法让它在不实例化我试图使用的类的新对象的情况下从应用程序中访问或修改类变量。这对我想做的事是不可接受的。json文件包含在一个名为MyJson.json的文件中,my_json是实际的数据。我知道这是可行的,因为设置面板会很好地弹出

我想使用一个设置面板,因为它的布局在这里或在一个类似的方式。但是,我需要在设置面板中更改这些值,以便在myScreen中的变量发生变化时进行更改

是否可以像我在下面的示例中所做的那样,在不创建MySScreen的新对象的情况下从应用程序访问MyScreensScreen

import sys
import os
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.config import Config
from kivy.config import ConfigParser
from kivy.uix.settings import SettingsWithSidebar
from kivy.uix.button import Button
from MyJson import my_json

class myScreen(Screen):
    def __init__(self, **kwargs):
        super(myScreen, self).__init__(**kwargs)
        self.test_var = 2

    def example_func(self):
        print(str(self.test_var))

class myScreenManager(ScreenManager):
    pass


root_widget = Builder.load_string('''
myScreenManager:
    myScreen:

<myScreen>:
    Button:
        id: test_button
        size_hint: .03, .05 
        pos_hint: {'x': .50, 'y': .50}
        on_press: app.open_settings()

''')

class TestApp(App):
    def build(self):
        self.settings_cls = SettingsWithSidebar
        self.temp = myScreen()
        return root_widget 

    def build_config(self, config):
        config.setdefaults('settings_stuff', {
            'example_one': False, 
            'example_two': True
            })

    def build_settings(self, settings):
        settings.add_json_panel('Example Panel', self.config, data = my_json)

    def on_config_change(self, config, section, key, value):
        if key == 'example_one' and value == '1':
            self.temp.test_var += 1
            self.temp.example_func()
            print(self.temp.test_var)
        elif key == 'example_one' and value == '0':
            self.temp.example_func()
            self.temp.test_var -= 1
            print(self.temp.test_var) 

if __name__ == '__main__':
     TestApp().run()
由于根目录是ScreenManager,因此可以使用get_Screen通过其名称访问屏幕:


您可以共享my_json,如果不按要求添加可变json代码,就不可能测试代码。它存储在一个名为MyJson的文件中。py@Afflicted一个建议,课程应该以大写字母开始,我在myScreen上遇到了很多问题。很好的建议,我一直都是这样做的:这是我刚刚举的一个例子,因为实际代码超过10k行。我想我在挫折中错过了它hehe@Afflicted一个项目可以有数百万行,对于每个文件,它必须有最多200行,否则它是不正确的,很难调试,最好是模块化的。并尝试证明您发布的代码是正确的,对其进行测试,很多时候,我们不希望看到一个凌乱而广泛的代码,它的代码不广泛,但有这个问题,我也不希望看到。总之,总是试着展示最好的例子,很多人会想要并且可以帮助你,否则很少有人会回顾你的问题。
import json 
my_json = json.dumps([
    {
         "type": "title",
         "title": "Settings Panel"
    },
    {
         "type": "bool",
         "title": "Add",
         "desc": "increment value",
         "section": "settings_stuff",
         "key": "example_one"  
    },
    {
         "type": "bool",
         "title": "random",
         "desc": "filler",
         "section": "settings_stuff",
         "key": "example_two"  
    }
])
import sys
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.settings import SettingsWithSidebar
from MyJson import my_json

class MyScreen(Screen):
    def __init__(self, **kwargs):
        super(MyScreen, self).__init__(**kwargs)
        self.test_var = 2

    def example_func(self):
        print(str(self.test_var))

class MyScreenManager(ScreenManager):
    pass


root_widget = Builder.load_string('''
MyScreenManager:
    MyScreen:
        name: 'name_of_screen'

<MyScreen>:
    Button:
        id: test_button
        size_hint: .03, .05 
        pos_hint: {'x': .50, 'y': .50}
        on_press: app.open_settings()

''')

class TestApp(App):
    def build(self):
        self.settings_cls = SettingsWithSidebar
        self.temp = root_widget.get_screen('name_of_screen')
        return root_widget 

    def build_config(self, config):
        config.setdefaults('settings_stuff', {
            'example_one': False, 
            'example_two': True
            })

    def build_settings(self, settings):
        settings.add_json_panel('Example Panel', self.config, data = my_json)

    def on_config_change(self, config, section, key, value):
        if key == 'example_one' and value == '1':
            self.temp.test_var += 1
            self.temp.example_func()
            print(self.temp.test_var)
        elif key == 'example_one' and value == '0':
            self.temp.example_func()
            self.temp.test_var -= 1
            print(self.temp.test_var) 

if __name__ == '__main__':
     TestApp().run()