Kivy添加小部件参数错误

时间:2016-01-17 17:55:19

标签: python csv widget kivy

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

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 EvaluateScreen(屏幕):         通

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)

1 个答案:

答案 0 :(得分:0)

你正在使用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