我在Kivy Python类上是新手

问题描述:

我是新奇的 我试图做包括

Im new on kivy I trying to do file.kv including

AddLocationForm:
<AddLocationForm@BoxLayout>:
    orientation:'vertical'
    Button:
        on_press:
            Test()
<Test@BoxLayout>:
    Label:
        text:"button was clicked"

当我单击按钮时我想要 其他类上的函数执行

I want that when i click on button The function on other class execute

在Kivy中,当您定义这样的小部件时:

In Kivy, when you define a widget like this:

<SomeWidget>:

您正在定义根窗口小部件规则,此窗口小部件只能影响其中的子窗口小部件.

You are defining a root widget rule, this widget can only affect children widget inside it.

例如:

<SomeWidget>:
    BoxLayout:
        Button:
            text: 'Test Two'
        Button:
            text: 'Test One'

要影响Root Widget中的属性,您需要使用ID.有两种方法可以执行此操作,您可以执行root.ids [X] .property

To affect a property within a Root Widget, you need to use ids. There are two ways to do this, you can do root.ids[X].property

在X是小部件的位置的地方,您还可以使用自行创建的ID(我更喜欢的方法,因为它更清楚我在影响什么)

Where X is the position of the Widget, you can also use self-created ids (my prefer method since it is more clear what I'm affecting)

<SomeWidget>:
     BoxLayout:
         Button:
             id: ButtonOne
             text: 'Button One'
             on_press: print(self.text)
         Button:
             text:
             on_press: root.ids.ButtonOne.text = 'Button Two Was Clicked'

在这里,root是指顶层小部件或

In here, root refers to the top-level widget or 'root' widget which is encased in

<WIDGETNAMEHRE>: 

然后,ids本质上是一个存储所有IDs(包括子代IDs的字典),因此通常只需要root.ids.button而不是root.ids.buttonparent.ids.button即可.

Then ids is essentially a dictionary that stores all ids (including the ids of children so you only need to go root.ids.button and not root.ids.buttonparent.ids.button, typically speaking)

您在示例中定义了两个根窗口小部件.认为它们的最佳方法是将它们视为类(因为本质上就是它们).当您拥有python类时,您不会直接使用该类,而是在该类中创建一个 from 对象.

You have two root widgets defined in your example. The best way to think of these are treat them as classes (since that is essentially what they are). When you have a python class, you don't use the class directly, you create a an Object from the class.

因此,您需要做的是将另一个Widget类的对象添加到父Widget.

So what you need to do is add an Object of the other Widget Class to your parent Widget.

例如

<TestWidget>:
    Button:
        text: 'change text'
        on_press: root.parent.parent.ids.ButtonOne.text = 'Button Was Clicked'

<PrimaryWidget>:
    BoxLayout:
        TestWidget:
        Button:
            id: ButtonOne
            text: 'Click the other one'

在这种情况下,当我们单击按钮"时,我们首先要进入根目录(即TestWidget),然后获取TestWidget的父级(在本例中为主要"下的BoxLayout),然后得到父级". (PrimaryWidget),然后我们访问ButtonOne所属的PrimaryWidget的ID.

In this case, when we click the Button, we're first going to root (ie TestWidget), then we're getting TestWidget's parent (in this case the BoxLayout under Primary), then we're getting the Parent of that (PrimaryWidget), then we're accessing the ids of PrimaryWidget to which ButtonOne is of.

您还可以通过应用程序实例访问内容,并从主窗口小部件向下进行操作.

You can also access things through the app instance as well and work downwards from your main widget.

<TestWidget>:
    Button:
        text: 'change text'
        on_press: app.root.ids.ButtonOne.text = 'Button Was Clicked'

在这种情况下,我们转到应用程序,然后转到应用程序的根窗口小部件(PrimaryWidget,基本上是在def build(self)方法中返回的kv文件,然后我们访问ButtonOne的ID.

In this case, we go to app, then app's root widget (the PrimaryWidget, basically the kv file that gets returned in the def build(self) method, then we access ids of ButtonOne.

关于ID的最后一点也是,您也可以访问自定义子窗口小部件的ID,例如:

One last bit on ids too is that you can access the ids of custom child widgets as well, for example:

<TestWidget>:
    Button:
        id: ButtonTwo
        text: 'change text'
        on_press: app.root.ids.ButtonOne.text = 'Button Was Clicked'

<PrimaryWidget>:
    BoxLayout:
        TestWidget:
            id: test
        Button:
            id: ButtonOne
            text: 'Click this one'
            on_press: root.ids.test.ids.ButtonTwo.text = 'The other button was clicked'

因此,在这种情况下,我们将以root用户身份(primarywidget)进入,然后以'test'身份访问TestWidget id,然后从其id中访问其ButtonTwo.

So in this case, we're going to root (primarywidget) then we're accessing the TestWidget id'ed as 'test' then we're accessing its ButtonTwo from its ids.

TestWidget在这里需要自己的ID的原因是,如果我们使用多个TestWidget,则需要指定我们要更改的TestWidget的ButtonTwo文本(否则,理论上,当我们只希望更改一个时,它们都可以更改)

The reason TestWidget requires its own id here, is because if we use more than one TestWidget, we need to specify which TestWidget's ButtonTwo text we want to change (otherwise, theoretically they could all change when we only want one to change).

由于您似乎正在尝试从python代码访问已定义的函数,因此您所需要做的就是找到其中包含的相关根窗口小部件并使用地址:

Since you seem to be trying to access an already defined function from your python code, all you need to do is to find the relevant root widget that it is contained in and use the address:

假设该函数包含在TestWidget中,并且您想从非testwidget按钮执行该函数以更改testwidget按钮文本,则可以在python类文件中执行以下操作:

Say the function was contained with TestWidget and you wanted to execute it from the non testwidget button to change testwidget button text, you would do something like this in the python class file:

class PrimaryWidget(Layoutofyourchoice):

    def change_text(self):
       self.ids.test.ids.ButtonTwo.text = 'CHANGED TEXT'

然后在您的kv文件中执行以下操作:

Then in your kv file, you would do:

<PrimaryWidget>:
     BoxLayout:
         TestWidget:
             id: Test
         Button:
             on_press: root.change_text()

希望这对您有所帮助!

I hope this helped you!