在Python中定义时,在KV文件上访问Button

时间:2017-10-14 16:49:53

标签: python python-3.x kivy kivy-language

我想更改KV文件中按钮的背景颜色,但它不起作用。按钮已定义并从Python文件循环

def gotoPost(i, instance):
    print("HAHAHAH")
    Manager.current = 'ScreenTwo'

class ScreenOne(Screen):
    box = ObjectProperty(None)

    def on_box(self, *args):
        for i in range(5):
            btn = Button(text=str(i))
            btn.bind(on_press=partial(gotoPost,i))
            self.box.add_widget(btn)

Factory.register('ScreenOne', cls=ScreenOne)

class ScreenTwo(Screen):
    pass

class Manager(ScreenManager):

    screen_one = ObjectProperty(None)
    screen_two = ObjectProperty(None)

class MainApp(App):
    def build(self):
        return Manager()
if __name__=="__main__":
    MainApp().run()

如何访问KV文件中的按钮?

1 个答案:

答案 0 :(得分:0)

class ScreenOne(Screen):
    box = ObjectProperty(None)
    color = [1.0,1.0,1.0,1.0]
    back_color = [0.0,0.0,0.0,1.0]
    def on_box(self, *args):
        for i in range(5):
            btn = Button(text=str(i), color= self.color, background_color= self.back_color)
            btn.bind(on_press=partial(gotoPost,i))
            self.box.add_widget(btn)

按下时,您可以指定按钮以根据需要更改颜色。例如:self.color或self.back_color =(7,0,1,1)......我认为那是红色的