为什么 kivy 构建器在尝试从文件 "look.kv" 中加载 "Logic" 小部件时失败

Why is the kivy builder is failng when trying to load the "Logic" widget from the file "look.kv" for the code included

我收到 TypeError: init() 在 Kivy

中得到了一个意外的关键字参数 '__no_builder'

我相信这正是 abrartx 于 11 月 2 日在 3:59 提出的问题,但该问题没有明确的答案。此代码是从 git 集线器下载并修改为使用伪随机数而不是麦克风的音频电平。

我在 windows 10

上使用 python 3.6.7 64 位和 kivy

我已经解决了几个依赖性问题,但这个让我卡住了。看起来 kivy 构建器无法在 look.kv 文件中构建“Logic”小部件。 任何帮助,将不胜感激。代码如下:

文件:main.py

#Real time plotting of Microphone level using kivy

from kivy.lang import Builder
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.garden.graph import MeshLinePlot
from kivy.clock import Clock
from threading import Thread
#import audioop
#import pyaudio
import random

def get_fake_mic_level():
    #source: 
    #audioop.max alternative to audioop.rms

    global levels
    while True:
        mx = random.random()
        if len(levels) >= 100:
            levels = []
        levels.append(mx)


class Logic(BoxLayout):
    def __init__(self,):
        super(Logic, self).__init__()
        self.plot = MeshLinePlot(color=[1, 0, 0, 1])

    def start(self):
        self.ids.graph.add_plot(self.plot)
        Clock.schedule_interval(self.get_value, 0.001)

    def stop(self):
        Clock.unschedule(self.get_value)

    def get_value(self, dt):
        self.plot.points = [(i, j/5) for i, j in enumerate(levels)]


class RealTimeMicrophone(App):
    def build(self):
        return Builder.load_file("look.kv")

if __name__ == "__main__":
    levels = []  # store levels of microphone
    #get_level_thread = Thread(target = get_microphone_level)
    get_level_thread = Thread(target = get_fake_mic_level)
    get_level_thread.daemon = True
    get_level_thread.start()
    RealTimeMicrophone().run()

文件:look.kv

#:import MeshLinePlot kivy.garden.graph.MeshLinePlot
Logic:
    BoxLayout:
        orientation: "vertical"
        BoxLayout:
            size_hint: [1, .8]
            Graph:
                id: graph
                xlabel: ""
                ylabel: "Level"
        BoxLayout:
            size_hint: [1, .2]
            orientation: "horizontal"
            Button:
                text: "START"
                bold: True
                on_press: root.start()
            Button:
                text: "STOP"
                bold: True
                on_press: root.stop()

堆栈:

File "D:[=14=]-Data[=14=]-code\py\audio-graph\real-time-plot-microphone-kivy-master\main.py", line 78, in <module>
  RealTimeMicrophone().run()
File "E:\Programs\Python-3-6-7\Lib\site-packages\kivy\app.py", line 949, in run
  self._run_prepare()
File "E:\Programs\Python-3-6-7\Lib\site-packages\kivy\app.py", line 919, in _run_prepare
  root = self.build()
File "D:[=14=]-Data[=14=]-code\py\audio-graph\real-time-plot-microphone-kivy-master\main.py", line 70, in build
  return Builder.load_file("look.kv")
File "E:\Programs\Python-3-6-7\Lib\site-packages\kivy\lang\builder.py", line 306, in load_file
  return self.load_string(data, **kwargs)
File "E:\Programs\Python-3-6-7\Lib\site-packages\kivy\lang\builder.py", line 404, in load_string
  widget = Factory.get(parser.root.name)(__no_builder=True)

builtins.TypeError: __init__() got an unexpected keyword argument '__no_builder'

您需要在 __init__() 方法中为您的 Logic class 处理关键字参数。像这样:

class Logic(BoxLayout):
    def __init__(self, **kwargs):
        super(Logic, self).__init__(**kwargs)
        self.plot = MeshLinePlot(color=[1, 0, 0, 1])