Kivy 添加小部件参数错误

Kivy adding widget arguments error

当我尝试使用从 .csv 文件检索到的变量 'arrowscore' 添加标签时,我遇到了错误:

TypeError: add_widget() missing 1 required positional argument: 'widget'

这是我的基本代码:

class EvaluateScreen(Screen):
pass


class EvaluateLayout(BoxLayout):
    global endNo
    def update(stuff):
        def readMe(endNo, i):
            f = open("end" + str(endNo) + ".csv", "r")  # opens the csv
            lines = [line.rstrip('\n') for line in f] #reads the lines of the file
            del lines[0] #removes the meta-value inside of the file
            lines = lines[0] #extracts a 1d array (single line) from the lines
            lines = lines.split(",") #splits the csv line into separate values in a list
            print(lines) #for debugging purposes
            f.close()  # closes the file
            return lines[int(i)] # returns the requested value

        if canDrawScores == True: #if an external variable is true
            for i in range(0,5): #for values 0 to 5
                arrowScore = readMe(endNo, i)
                BoxLayout.add_widget(Label(text=str(arrowScore))) #add a new label to the BoxLayout with text as the retrieved value

    Clock.schedule_interval(update, 0.5) #checking if the external variable is true every half second

--- 已编辑但仍然损坏 ---- 现在代码在 self.do_layout 中引发 AttributeError,指出 'float' 对象没有属性 'do_layout' class 评估屏幕(屏幕): 通过

class EvaluateLayout(BoxLayout):
    global endNo
    global endLimit
    def update(self):
        self.do_layout()
        def readMe(endNo, i):
             f = open("end" + str(endNo) + ".csv", "r")  # opens the end
            lines = [line.rstrip('\n') for line in f] #reads the lines of the file
            del lines[0]
            lines = lines[0]
            lines = lines.split(",")
            print(lines)
            f.close()  # closes the file
            return lines[int(i)] # returns the requested value

        if canDrawScores == True:
            for i in range(0,endLimit):
               arrowScore = readMe(endNo, i)
               self.add_widget(Label(text=str(arrowScore)))

Clock.schedule_interval(update, 0.5)


evaluatescreen = EvaluateScreen()
evaluatelayout = EvaluateLayout()
evaluatescreen.add_widget(evaluatelayout)

您正在使用 BoxLayout(类型)而不是 self(BoxLayout 的一个实例)

 def update(stuff):

     BoxLayout.add_widget(Label(text=str(arrowScore))) #add a new label to the BoxLayout with text as the retrieved value

改为...

 def update(self, dt): #<---changed stuff to self and added dt (delta_time)

     self.add_widget(Label(text=str(arrowScore))) #add a new label to the BoxLayout with text as the retrieved value

现在最后一个问题:

Clock.schedule_interval(update, 0.5) #checking if the external variable is true every half second

这是在 EvaluateLayout.update 方法上完成的,而不是在 EvaluateLayout instance.update 上完成的......你只需要这些行

el = EvaluateLayout()#creating the layout here...
#and later on...
Clock.schedule_interval(el.update, 0.5) #checking if the external variable is