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

问题描述

我试图了解kivy .kv文件是如何工作的,所以我创建了一个带有水平BoxLayout的小型应用程序,其中包含三个GridLayouts,如下所示:

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或类似的字词,但首个大写字母似乎是重要.我什至遇到过这样的情况,可悲的是,单词中的大写字母足够,其余的都小写,但无法复制.

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布局放入应用程序中:

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文件中复制小部件块(仅小写规则)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 16:04