【问题标题】:Accessing Button on KV file when its defined in a Python在 Python 中定义时访问 KV 文件上的按钮
【发布时间】:2017-10-14 16:49:53
【问题描述】:

我想更改我的 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 文件中的按钮?

【问题讨论】:

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


    【解决方案1】:
    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)... 我认为那是红色

    【讨论】:

    • 问题是“如何访问 KV 文件中的按钮?”而不是在 Python 脚本中。
    猜你喜欢
    • 2018-05-16
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多