【问题标题】:Im New on kivy Python classes我是 kivy Python 课程的新手
【发布时间】:2018-01-18 16:13:19
【问题描述】:

我是 kivy 的新手 我试图做 file.kv 包括

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

当我点击按钮时我想要那个 其他类的函数执行

【问题讨论】:

标签: python kivy-language


【解决方案1】:

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

<SomeWidget>:

你正在定义一个根小部件规则,这个小部件只能影响它里面的子小部件。

例如:

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

要影响 Root Widget 中的属性,您需要使用 id。有两种方法可以做到这一点,你可以做root.ids[X].property

其中X是Widget的位置,你也可以使用自己创建的id(我更喜欢的方法,因为它更清楚我在影响什么)

<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 是指封装在

中的顶级小部件或“根”小部件
<WIDGETNAMEHRE>: 

那么 ids 本质上就是一个存储所有 ids 的字典(包括孩子的 ids 所以你只需要去 root.ids.button 而不是 root.ids.buttonparent.ids.button,通常来说)

您的示例中定义了两个根小部件。考虑这些的最好方法是将它们视为类(因为它们本质上就是这样)。当你有一个 python 类时,你不直接使用该类,而是创建一个 Object from 类。

因此,您需要做的是将另一个 Widget 类的 Object 添加到您的父 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'

在这种情况下,当我们单击 Button 时,我们首先会进入 root(即 TestWidget),然后我们会得到 TestWidget 的父级(在本例中是 Primary 下的 BoxLayout),然后我们会得到 Parent那(PrimaryWidget),然后我们正在访问 ButtonOne 所在的 PrimaryWidget 的 id。

您也可以通过应用实例访问事物,并从您的主小部件向下工作。

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

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

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

<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),然后访问 ID 为“test”的 TestWidget,然后从其 ID 访问其 ButtonTwo。

这里TestWidget需要自己的id的原因,是因为如果我们使用多个TestWidget,我们需要指定我们想要改变哪个TestWidget的ButtonTwo文本(否则理论上我们只想改变一个时它们都可以改变) .

由于您似乎试图从您的 python 代码中访问一个已经定义的函数,您需要做的就是找到它所包含的相关根小部件并使用该地址:

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

class PrimaryWidget(Layoutofyourchoice):

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

然后在你的 kv 文件中,你会这样做:

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

希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-25
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多