Kivy通过加载.kv文件的代码创建小部件

时间:2016-03-04 15:08:34

标签: python popup kivy

我想为用户第一次打开应用时创建一个弹出式教程。因为我希望本教程尽可能与应用程序其余部分的代码分开(并且仅在使用教程时加载),我将代码分开:

在同一个文件夹中:

  • foo.py
  • foo.kv
  • learnkanji_tut.py
  • learnkanji_tut.kv

编辑foo.py

foo.py(其中包含.kv文件ID:lfunc):

class LayoutFunctioning(BoxLayout):
    def __init__(self, **kwargs):
        super(LayoutFunctioning, self).__init__(**kwargs)

        tutorial = 1

        if tutorial:
            Clock.schedule_once(lambda dt: self.tutwidget())

    def tutwidget(self):         
        import learnkanji_tut
        tutpop = learnkanji_tut.TutInteraction()
        self.ids.lfunc.add_widget(tutpop)

learnkanji_tut.py:

#!/usr/bin/kivy
# -*- coding: utf-8 -*-

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.popup import Popup
from os.path import join, dirname


class TutInteraction(Popup):
    curdir = dirname(__file__)
    Builder.load_file(join(curdir, 'learnkanji_tut.kv'))
    tut_text = "Tut"

    def __init__(self, **kwargs):
        self.title = 'Tutorial Popup'
        self.auto_dismiss = 'False'
        super(TutInteraction, self).__init__(**kwargs)

        print("Tutorial loaded")
        self.tut_step = 0  # Current step in the tutorial

learnkanji_tut.kv:

#:kivy 1.9.1

<TutInteraction>:
    id: tutpop

    BoxLayout:
        ScrollView:
            size_hint_y: 0.8
            text: "test"

        BoxLayout:
            size_hint_y: 0.2

            Button:
                size_hint_x: 0.3
                text: "Skip"

            Button:
                size_hint_x: 0.7
                text: "Next"

错误:

self.ids.lfunc.add_widget(tutpop) File "kivy\properties.pyx", line 757, in kivy.properties.ObservableDict.__getattr__ (kivy\properties.c:11882) AttributeError: 'super' object has no attribute '__getattr__'

问题:

我做错了什么?为什么我不能添加像这样的弹出窗口小部件? 按照文档:https://kivy.org/docs/api-kivy.uix.popup.html,Popup应该有:Popup.title and Popup.content

我有一个标题(self.title = 'Tutorial Popup')和内容(.kv文件)。那我在这里或其他地方做错了什么?

1 个答案:

答案 0 :(得分:0)

来自IRC频道的@inclement #kivy:“不要将add_widget与Popup一起使用,调用它的.open()方法”

所以这意味着:

if tutorial:
    import learnkanji_tut
    tutpop = learnkanji_tut.TutInteraction()
    tutpop.open()

P.S。似乎Clock.schedule_once()不是必需的。