文件中复制小部件块

文件中复制小部件块

本文介绍了如何在 kv 文件中复制小部件块(仅小写规则)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解 kivy .kv 文件的工作原理,因此我创建了一个带有水平 BoxLayout 的小应用程序,其中包含三个 GridLayout,如下所示:

I try to understand how kivy .kv files work, so I created a small application with a horizontial BoxLayout which contains three GridLayouts, as shown:

my_widget:

<my_widget@BoxLayout>:
    orientation: "horizontal"

    GridLayout:
        rows: 3
        ToggleButton:
        Image:
        Label:

    GridLayout:
        rows: 3
        ToggleButton:
        Image:
        Label:

    GridLayout:
        rows: 3
        ToggleButton:
        Image:
        Label:

那里没问题,但是由于有相同的小部件块(GridLayouts),它们可以复制吗?我尝试了类似:https://kivy.org/docs/api-kivy.lang.html

No problem there, but since there are same blocks of widgets (GridLayouts) could they be duplicated? I tried something like: https://kivy.org/docs/api-kivy.lang.html

my_widget:

[my_widget2@GridLayout]:
    rows: 3
    ToggleButton:
    Image:
    Label:

<my_widget@BoxLayout>:
    orientation: "horizontal"

    my_widget2:
    my_widget2:
    my_widget2:

但是没有用.如果可以复制,那么如何将信息传递给块中的每个小部件?

but didn't work. If duplicating is possible, then how can I pass information to each one widget in the block?

推荐答案

是的,这让我每次遇到它都很难过,例如将单词复制并粘贴到 <> 中(忘记大写).我仍然想知道是否应该将其视为错误或功能,因为它强制用户以正确的样式/大小写命名小部件,这也更易于阅读.

Yeah, this gets me badly everytime I encounter it e.g. copy&paste word into <> (and forget about uppercase). I still wonder if I should treat it as a bug or as a feature, because it forces user to name widgets in a correct style/case, which makes it easier for reading too.

问题是kv 语言中的小部件/规则应该使用ThisWordStyle 或类似的东西,但第一个大写 字母似乎是important.我什至遇到了一个情况,单词中的一个大写字母就足够了,其余的小写字母,但遗憾的是无法重现.

The thing is that widgets/rules in kv language should use ThisWordStyle or something similar, but the first capital letter seems to be important. I encountered even a case when a capital letter inside the word was enough and the rest lowercase, but can't reproduce, sadly.

只有小写的单词主要用作属性或变量,所以可能 my_widget 被当作属性或变量处理太像 global,或者在运行语言解析器.

Words with lowercase only are mostly used as properties or variables, so maybe the my_widget was handled as a property or variable too like a global, or was completely ignored when run through the language parser.

让我们看看:

1)你的kv布局放入App:

1) your kv layout put into App:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
Builder.load_string('''
<my_widget@BoxLayout>:
    my_widget2:
    Button:
        text: 'bla'

<MyWidget>:
    my_widget:
    Button:
        text: 'bad'

<my_widget2@GridLayout>:
    rows: 3
    ToggleButton:
    Image:
    Label:

''')
class MyWidget(BoxLayout):
    pass

class Test(App):
    def build(self):
        return MyWidget()
Test().run()

唯一可见的将是带有 'bad' 字符串的 Button

The only visible thing will be a Button with 'bad' string

2) 命名稍有变化 - my_widget -> My_widget

2) a little change in naming - my_widget -> My_widget

<My_widget@BoxLayout>:
    my_widget2:
    Button:
        text: 'bla'

<MyWidget>:
    My_widget:
    Button:
        text: 'bad'

<my_widget2@GridLayout>:
    rows: 3
    ToggleButton:
    Image:
    Label:

还有一个可见的小部件!

and there's one more visible widget!

3) 包含所有内容的工作布局 (my_widget2 -> My_widget2)

3) a working layout with all stuff (my_widget2 -> My_widget2)

<My_widget@BoxLayout>:
    My_widget2:
    My_widget2:
    Button:
        text: 'bla'

<MyWidget>:
    My_widget:
    Button:
        text: 'bad'

<My_widget2@GridLayout>:
    rows: 3
    ToggleButton:
    Image:
    Label:

还要回答将参数传递到此类小部件 (<My@Widget>),请使用 Factory 来访问这样的小部件,然后它只传递 (kw)args:

Also to answer passing arguments into such widgets (<My@Widget>), use Factory to access such widget and then it's only passing (kw)args:

#:import Factory kivy.factory.Factory
<MyWidget>:
    Button:
        on_release: root.add_widget(Factory.My_widget2(text='hi'))

<My_widget2@Label>:
    size_hint: [None, None]
    size: [200, 50]

这篇关于如何在 kv 文件中复制小部件块(仅小写规则)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 10:43