我试图使用kivy的设置面板。但是,我无法在App()中访问和修改类变量,而无需实例化I类试图使用的新对象。对于我想做的事情,这是不可接受的。 json文件包含在一个名为MyJson.json的文件中,my_json是实际数据。我知道这很有效,因为设置面板弹出正常。
我想在此处(或以类似的方式)使用设置面板。但是,我需要在设置面板中更改的值来更改myScreen中的变量。
是否可以从App()访问myScreen(屏幕)而不创建myScreen()的新对象,如下面的示例所示?
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()
如果我运行上面的代码和&#34;打开&#34;它调用函数的开关并打印值2而不是3。它调用该函数,但它不会在活动屏幕类中实现更改。
Json代码在这里:
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"
}
])
答案 0 :(得分:1)
由于根是ScreenManager
,您可以使用name
通过get_screen()
访问屏幕:
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()