首页 > 解决方案 > Kivy(Python)在使用 kv 文件的 add_widget 后出现 AttributeError 问题

问题描述

我是 Kivy 的新手并且有以下问题(环境是 Python 3.7 和 Kivy-1.11.1。):

我需要一个导航区和一个视图区(=ViewScreen)。通过导航区域,我更改了视图区域(更改 kv 文件 - 稍后查看“def next_screen”)。我的问题是,我无法与视图区域中的小部件(例如“lblTest”)进行交互。我使用以下文件:

GUI 应用程序很简单,可以启动 GUI 并更改视图区域。 测试GUI.py:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout

class RootWidget(BoxLayout):
   # runs select application
   def startApplication (self, instance):
      print(self.lblTest)

class mainGUI(App):
   def build(self):
      # loading the content of root.kv
      self.root = Builder.load_file('testGUIRoot.kv')

   def next_screen(self, screen):
      #Clear container and load the given screen object from file in kv folder.

      filename = screen + '.kv'
      # unload the content of the .kv file
      # reason: it could have data from previous calls
      Builder.unload_file(filename)

      # clear the container
      self.root.container.clear_widgets()
      # load the content of the .kv file
      screen = Builder.load_file(filename)
      # add the content of the .kv file to the container
      self.root.container.add_widget(screen)

if __name__ == '__main__':
   '''Start the application'''
   mainGUI().run()

我使用以下 kv 文件: testGUIRoot.kv:

#:kivy 1.11.1

RootWidget:
   container: container
   orientation: 'horizontal'

   # Navigation
   BoxLayout:
      orientation: 'vertical'
      size_hint: (0.35, 1)

      Button:
         text: 'testButton1'
         on_release: root.startApplication(self,)
         on_press: app.next_screen('testGUIBtn1')

      Button:
         text: 'testButton2'
         on_release: root.startApplication(self,)
         on_press: app.next_screen('testGUIBtn2')

   # ViewScreen
   BoxLayout:
      size_hint: (0.65, 1)
      id: container
      orientation: 'vertical'
      padding: 0
      spacing: 3

      Label:
         text: 'no data'
         color: (0.667,0.667,0.667,1)
         font_size: 14
         bold: True

测试GUIBtn1.kv:

#:kivy 1.11.1

Label:
   id: lblTest
   text: 'Button 1'
   color: (0.667,0.667,0.667,1)
   font_size: 14
   bold: True

测试GUIBtn2.kv:

#:kivy 1.11.1

Label:
   id: lblTest
   text: 'Button 2'
   color: (0.667,0.667,0.667,1)
   font_size: 14
   bold: True

出现跟随错误: AttributeError: 'RootWidget' object has no attribute 'lblTest'

您有与对象“lblTest”交互的解决方案吗?例如像 self.lblTest.text = 'Test-Text'。

先感谢您

标签: pythonkivy

解决方案


就在这周,我一直在与 Kivy 合作,也是作为新用户。我学到的是在你的 RootWidget 上定义属性,就像你已经在 .kv 文件中定义的标签一样。这将布局和 Python 代码“链接”到彼此。

首先,您需要通过添加以下内容来导入 Kivy ObjectProperty: from kivy.properties import ObjectProperty

接下来是在您的 RootWidget 类上声明该属性。您可以lblTest = ObjectProperty(None)在类声明之后添加。

文件 testGUI.py 的顶部应如下所示:

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout


class RootWidget(BoxLayout):
   # Link the layout objects
   lblTest = ObjectProperty(None)

   # runs select application
   def startApplication (self, instance):
      print(self.lblTest)

真正帮助我的页面是https://techwithtim.net/tutorials/kivy-tutorial/object-properties/

一点旁注是最好保持您的id属性完全独特。


推荐阅读