简体   繁体   English

猕猴桃外部规则继承2

[英]Kivy outside rule inherence 2

As follow-up question to: 作为后续问题:

main.py main.py

import os
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.stacklayout import StackLayout
from kivy.properties import ObjectProperty


class FancyButton(Button):
    imp = ObjectProperty(None)


class Important(StackLayout):

    font_kanji = os.path.join('fonts', 'TakaoPMincho.ttf')

    def NoInspiration(self, smile):
        print("Received: {}".format(smile))

    def AddFancy(self):
        print(self.ids)
        temp = FancyButton(text='f', imp = self)
        self.ids.boxy.add_widget(temp)


class TestApp(App):
    def build(self):
        pass

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

test.kv 测试文件

#:kivy 1.9.0

<FancyButton>:
    font_name: self.imp.font_kanji  # ERROR
    on_release: self.imp.NoInspiration(':)')  # WORKS


<Important>:
    id: imp

    BoxLayout:
        id: boxy
        orientation: 'vertical'

        FancyButton:
            text: "smiley"
            imp: root

        Button:
            text: "add fancy"
            on_release: imp.AddFancy()


BoxLayout:
    Important

In the above example 'on_release: self.imp.NoInspiration(':)')' works because FancyButton has 'imp: root'. 在上面的示例中,“ on_release:self.imp.NoInspiration(':)')'有效是因为FancyButton具有“ imp:root”。

However 'font_name: self.imp.font_kanji' doesn't work and gives the error: 但是'font_name:self.imp.font_kanji'不起作用,并显示错误消息:

AttributeError: 'NoneType' object has no attribute 'font_kanji' AttributeError:“ NoneType”对象没有属性“ font_kanji”

My guess is the reason for this is that on_release takes place after all widgets are loaded and font_name directly, so without having 'imp: root'. 我的猜测是这样做的原因是on_release是在所有小部件都直接加载后和font_name直接加载之后发生的,因此没有'imp:root'。

I also tried: 我也尝试过:

font_kanji = StringProperty(os.path.join('fonts', 'TakaoPMincho.ttf'))

, but to no avail. ,但无济于事。

Question

How do I get font_name refer to font_kanji? 我如何获得font_name引用font_kanji? Should I use global? 我应该使用全局吗? If yes, how would you set a global in Python which can be accessed in the .kv? 如果是,如何在Python中设置一个可以在.kv中访问的全局变量?

(If I put global in front of font_Kanji and remove 'self.imp' in the .kv file I get the error: " NameError: name 'font_kanji' is not defined") (如果我将global放在font_Kanji的前面,并在.kv文件中删除“ self.imp”,则会出现错误:“ NameError:未定义名称'font_kanji'”)

Your guess is right: when your button is created its imp property is None . 您的猜测是正确的:创建按钮时,其imp属性为None One to go around this would be to observe imp property and set the value of font_name in its handler: 解决此问题的一种方法是观察imp属性并在其处理程序中设置font_name的值:

class FancyButton(Button):
    imp = ObjectProperty(None)

    def on_imp(self, obj, imp):
        if imp:
            self.font_name = imp.font_kanji

This way the font is set after imp property is initialzied with proper Important instance. 这样,在使用适当的Important实例初始化imp属性后,即可设置字体。 The dissadvantage of this method is that changes of Instance.font_kanji won't trigger changes of FancyButton.font_name . 此方法的缺点是Instance.font_kanji更改不会触发FancyButton.font_name更改。

If you want to have both properties binded, then you have to call bind funtion from Instance.font_kanji side (since we want to react to its changes) for dynamically created FancyButton instances: 如果要绑定两个属性,则必须为动态创建的FancyButton实例从Instance.font_kanji端调用bind功能(因为我们要对它的更改做出反应):

class Important(StackLayout):
    font_kanji = os.path.join('fonts', 'TakaoPMincho.ttf')

    def NoInspiration(self, smile):
        print("Received: {}".format(smile))

    def AddFancy(self):
        temp = FancyButton(text='f', imp = self)
        self.bind(font_kanji=temp.setter('font_name'))
        self.ids.boxy.add_widget(temp)

Interface defined in the kv language can do the binding directly: 用kv语言定义的接口可以直接进行绑定:

<Important>:
    id: imp

    BoxLayout:
        id: boxy
        orientation: 'vertical'

        FancyButton:
            text: "smiley"
            font_name: root.font_kanji
            imp: root

        Button:
            text: "add fancy"
            on_release: imp.AddFancy()
''')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM