【问题标题】:Organizing kivy layout(s) and classes组织 kivy 布局和类
【发布时间】:2020-12-02 00:57:40
【问题描述】:

我有一个相当大的应用程序拆分为一个 .kv 文件和一个 GUI.py 文件访问其他 .py 文件的库。我想组织所有内容并将大型布局拆分为不同的类和 .kv 文件。

目前我正在开发一个函数,该函数应该在我的主布局中添加和删除某个布局,同时仍然访问基类的变量和函数(称为 BoxL)。我尝试了各种方法,但我不知道如何将我的主类移交/实例化到/在我的新类中。

我正在尝试构建一个粗略的最小示例:

主要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)

我想从中访问 GUI.py 中 BoxL 类的函数/变量的新布局文件 layouts.kv:

<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

我希望这涵盖了所有内容。我为此苦苦挣扎了一段时间。

【问题讨论】:

    标签: class kivy root parent kivy-language


    【解决方案1】:

    想通了:app.root.FUNCTION()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 2016-09-14
      • 1970-01-01
      • 2014-08-24
      • 1970-01-01
      • 2021-09-24
      • 2013-12-29
      相关资源
      最近更新 更多