组织 kivy 布局和 类
Organizing kivy layout(s) and classes
我有一个相当大的应用程序拆分为一个 .kv 文件和一个 GUI.py 文件访问其他 .py 文件的库。我到了一个地步,我想组织所有内容并将大型布局拆分为不同的 classes 和 .kv 文件。
目前我正在开发一个函数,它应该在我的主布局中添加和删除某个布局,同时仍然访问基础 class(称为 BoxL)的变量和函数。我尝试了各种方法,但我不知道如何将 over/instantiate 我的主要 class to/in 我的新 class.
我正在尝试构建一个粗略的最小示例:
主 python 文件:GUI.py
import kivy
from kivy.app import App
from kivy.config import Config
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
class AdvancedClass(BoxLayout):
"""This is my new class from which i want to access BoxL class."""
pass
class BoxL(BoxLayout):
def __init__(self):
super(BoxL, self).__init__()
some_variable = 1
advanced_mode_enabled = False
def some_function(self):
print(self.some_variable)
print('Im also doing stuff in GUI.kv file with ids')
self.ids.test_label.text = '[b]some text with markup'
def advanced_mode(self):
if not self.advanced_mode_enabled:
print('Enabling advanced mode ..')
self.ids.master_box.add_widget(self.advanced_class_obj)
self.advanced_mode_enabled = True
else:
print('Disabling advanced mode ..')
self.ids.master_box.remove_widget(self.advanced_class_obj)
self.advanced_mode_enabled = False
class GUI(App):
def build(self):
Builder.load_file('layouts.kv') # i read its best to instanciate kivy files here once everything is loaded
BoxL.advanced_class_obj = AdvancedClass()
if __name__ == "__main__":
GUI().run()
主布局文件:GUI.kv
<BoxL>:
orientation: 'vertical'
id: master_box
Label:
text: str(root.some_variable)
Button:
text: 'Change variable'
on_release:
root.some_variable = 2
Button:
text: 'Add advanced layout'
on_release:
root.advanced_mode(self)
新布局文件 layouts.kv,我想从中访问 GUI.py 中 BoxL class 的 functions/variables:
<AdvancedClass>:
orientation: 'vertical'
Label:
text: '[b]TEST'
TextInput:
hint_text: 'TEST'
Button:
text: 'KLICK'
on_release:
# print(parent.some_variable)
# print(self.some_variable)
# print(BoxL.some_variable)
print('Im not sure how to do this .. ') # issue-point
我希望这涵盖了所有内容。我为此苦苦挣扎了一段时间。
计算出来:app.root.FUNCTION()
我有一个相当大的应用程序拆分为一个 .kv 文件和一个 GUI.py 文件访问其他 .py 文件的库。我到了一个地步,我想组织所有内容并将大型布局拆分为不同的 classes 和 .kv 文件。
目前我正在开发一个函数,它应该在我的主布局中添加和删除某个布局,同时仍然访问基础 class(称为 BoxL)的变量和函数。我尝试了各种方法,但我不知道如何将 over/instantiate 我的主要 class to/in 我的新 class.
我正在尝试构建一个粗略的最小示例:
主 python 文件:GUI.py
import kivy
from kivy.app import App
from kivy.config import Config
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
class AdvancedClass(BoxLayout):
"""This is my new class from which i want to access BoxL class."""
pass
class BoxL(BoxLayout):
def __init__(self):
super(BoxL, self).__init__()
some_variable = 1
advanced_mode_enabled = False
def some_function(self):
print(self.some_variable)
print('Im also doing stuff in GUI.kv file with ids')
self.ids.test_label.text = '[b]some text with markup'
def advanced_mode(self):
if not self.advanced_mode_enabled:
print('Enabling advanced mode ..')
self.ids.master_box.add_widget(self.advanced_class_obj)
self.advanced_mode_enabled = True
else:
print('Disabling advanced mode ..')
self.ids.master_box.remove_widget(self.advanced_class_obj)
self.advanced_mode_enabled = False
class GUI(App):
def build(self):
Builder.load_file('layouts.kv') # i read its best to instanciate kivy files here once everything is loaded
BoxL.advanced_class_obj = AdvancedClass()
if __name__ == "__main__":
GUI().run()
主布局文件:GUI.kv
<BoxL>:
orientation: 'vertical'
id: master_box
Label:
text: str(root.some_variable)
Button:
text: 'Change variable'
on_release:
root.some_variable = 2
Button:
text: 'Add advanced layout'
on_release:
root.advanced_mode(self)
新布局文件 layouts.kv,我想从中访问 GUI.py 中 BoxL class 的 functions/variables:
<AdvancedClass>:
orientation: 'vertical'
Label:
text: '[b]TEST'
TextInput:
hint_text: 'TEST'
Button:
text: 'KLICK'
on_release:
# print(parent.some_variable)
# print(self.some_variable)
# print(BoxL.some_variable)
print('Im not sure how to do this .. ') # issue-point
我希望这涵盖了所有内容。我为此苦苦挣扎了一段时间。
计算出来:app.root.FUNCTION()