<bdo id='mOrDn'></bdo><ul id='mOrDn'></ul>

    1. <legend id='mOrDn'><style id='mOrDn'><dir id='mOrDn'><q id='mOrDn'></q></dir></style></legend>
      <i id='mOrDn'><tr id='mOrDn'><dt id='mOrDn'><q id='mOrDn'><span id='mOrDn'><b id='mOrDn'><form id='mOrDn'><ins id='mOrDn'></ins><ul id='mOrDn'></ul><sub id='mOrDn'></sub></form><legend id='mOrDn'></legend><bdo id='mOrDn'><pre id='mOrDn'><center id='mOrDn'></center></pre></bdo></b><th id='mOrDn'></th></span></q></dt></tr></i><div id='mOrDn'><tfoot id='mOrDn'></tfoot><dl id='mOrDn'><fieldset id='mOrDn'></fieldset></dl></div>
    2. <tfoot id='mOrDn'></tfoot>
    3. <small id='mOrDn'></small><noframes id='mOrDn'>

      如何获取 kivy 按钮的 Id 和 Text 值作为字符串?

      时间:2023-10-10
      <legend id='He79B'><style id='He79B'><dir id='He79B'><q id='He79B'></q></dir></style></legend>
        <tbody id='He79B'></tbody>
    4. <tfoot id='He79B'></tfoot>
      <i id='He79B'><tr id='He79B'><dt id='He79B'><q id='He79B'><span id='He79B'><b id='He79B'><form id='He79B'><ins id='He79B'></ins><ul id='He79B'></ul><sub id='He79B'></sub></form><legend id='He79B'></legend><bdo id='He79B'><pre id='He79B'><center id='He79B'></center></pre></bdo></b><th id='He79B'></th></span></q></dt></tr></i><div id='He79B'><tfoot id='He79B'></tfoot><dl id='He79B'><fieldset id='He79B'></fieldset></dl></div>

            <bdo id='He79B'></bdo><ul id='He79B'></ul>

              <small id='He79B'></small><noframes id='He79B'>

              • 本文介绍了如何获取 kivy 按钮的 Id 和 Text 值作为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个带有多个按钮的应用程序,我需要在按下按钮时将按钮的 id 和文本值作为字符串获取.然后将抓取的按钮的 Ids 和 Text 值传递给另一个函数以进行进一步处理.为简单起见,我编写了这个示例程序.

                I have an app with multiple buttons and I need to get id and text value of the button as string when it is pressed. The grabbed Ids and Text valus of the button will then be passed to another function for further processing. For simplicity I wrote this sample programme.

                # main.py
                
                from kivy.app import App
                from kivy.uix.boxlayout import BoxLayout
                
                ########################################################################
                class KVMyHBoxLayout(BoxLayout):
                    pass
                
                
                ########################################################################
                class ExampleApp(App):
                    def Pressbtn(self, *args):
                        print'Pressed button'
                    #----------------------------------------------------------------------
                    def build(self):
                
                        return KVMyHBoxLayout()
                
                #----------------------------------------------------------------------
                if __name__ == "__main__":
                    app = ExampleApp()
                    app.run()
                

                kv文件

                <MyButton@Button>:
                    color: .8,.9,0,1
                    font_size: 32
                
                <KVMyHBoxLayout>:
                    orientation: 'vertical'
                    MyButton:
                        id:"idBtn1"
                        text: "Btn1"
                        background_color: 1,0,0,1
                        on_press:app.Pressbtn()
                    MyButton:
                        id:"idBtn2"
                        text: "Btn2"
                        background_color: 0,1,0,1
                        on_press:app.Pressbtn()
                    Label:
                        text: "ID"
                        background_color: 0,0,1,1
                    Label:
                        text: "Text"
                        background_color: 1,0,1,1
                

                当按下按钮时,其对应的 id 和 text 值将显示在 ID 和 Text 标签中.目前上述代码仅在按下按钮时打印Pressed button.我想知道如何以python方式获取按钮的id和文本值.

                When a button is pressed its corresponding values of id and text will be shown in the ID and Text labels. Currently the above code only print Pressed button when button is pressed. I want to know how to get id and text value of button pythonically.

                推荐答案

                首先,从kv调用时,必须将按钮实例显式传递给方法:

                First, you must pass the button instance explicitly to the method when it is called from the kv:

                on_press: app.Pressbtn(self)
                

                然后您可以使用实例引用来修改按钮或查看其属性,您不需要 id.如果要获取id,只能使用按钮父级的ids字典.

                You can then use the instance reference to modify the button or see its attributes, you do not need the id. If you want to get the id, you can only do it using the ids dictionary of the button parent.

                基于您的代码的示例:

                from kivy.app import App
                from kivy.uix.boxlayout import BoxLayout
                from kivy.lang import Builder
                
                kv_file = '''
                <MyButton@Button>:
                    color: .8,.9,0,1
                    font_size: 32
                
                <KVMyHBoxLayout>:
                    orientation: 'vertical'
                    MyButton:
                        id:"idBtn1"
                        text: "Btn1"
                        background_color: 1,0,0,1
                        on_press:app.Pressbtn(self)
                    MyButton:
                        id:"idBtn2"
                        text: "Btn2"
                        background_color: 0,1,0,1
                        on_press:app.Pressbtn(self)
                    Label:
                        id: lobj
                        text: "Object"
                        background_color: 1,0,1,1
                
                    Label:
                        id: lid
                        text: "ID"
                        background_color: 0,0,1,1
                    Label:
                        id: ltext
                        text: "Text"
                        background_color: 1,0,1,1
                '''
                
                
                class KVMyHBoxLayout(BoxLayout):
                    pass
                
                class ExampleApp(App):
                    def Pressbtn(self, instance):
                        instance.parent.ids.lobj.text = str(instance)
                        instance.parent.ids.ltext.text = instance.text
                        instance.parent.ids.lid.text= self.get_id(instance)
                
                    def get_id(self,  instance):
                        for id, widget in instance.parent.ids.items():
                            if widget.__self__ == instance:
                                return id
                
                    def build(self):
                        Builder.load_string(kv_file)
                        return KVMyHBoxLayout()
                
                if __name__ == "__main__":
                    app = ExampleApp()
                    app.run()
                

                输出:

                如果您在 .py 文件中定义小部件(按钮),则无需将实例传递给函数,它会自动作为参数传递:

                If you define the widget (button) in the .py file you do not need to pass the instance to the function, it is passed automatically as argument:

                from kivy.app import App
                from kivy.uix.boxlayout import BoxLayout
                from kivy.uix.screenmanager import ScreenManager, Screen
                from kivy.uix.button import Button
                from kivy.uix.scrollview import ScrollView
                
                
                class FirstScreen(Screen):
                    def __init__(self,**kwargs):
                        super(FirstScreen, self).__init__(**kwargs)    
                        layout=BoxLayout(orientation="vertical",size_hint_y= None)
                        layout.bind(minimum_height=layout.setter('height'))                
                        for i in range(50):
                                btn = Button(text="Button"+str(i),
                                             id=str(i),
                                             size_hint=(None, None),
                                             on_press=self.Press_auth)               #<<<<<<<<<<<<<<<<           
                                layout.add_widget(btn)
                        root = ScrollView()
                        root.add_widget(layout)
                        self.add_widget(root)
                
                    def Press_auth(self,instance):     
                        print(str(instance))
                
                class TestScreenManager(ScreenManager):
                    def __init__(self,  **kwargs):
                        super(TestScreenManager,  self).__init__(**kwargs)
                        self.add_widget(FirstScreen())
                
                class ExampleApp(App):
                    def build(self):           
                        return TestScreenManager()
                
                def main():
                    app = ExampleApp()
                    app.run()
                
                if __name__ == '__main__':
                    main()
                

                这篇关于如何获取 kivy 按钮的 Id 和 Text 值作为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Kivy 属性错误 - 对象没有属性 - 尝试以 kv 语言连 下一篇:Kivy:如何在等待显示另一个小部件时显示一个小部

                相关文章

                最新文章

                  <tfoot id='rNH4Y'></tfoot>
                1. <i id='rNH4Y'><tr id='rNH4Y'><dt id='rNH4Y'><q id='rNH4Y'><span id='rNH4Y'><b id='rNH4Y'><form id='rNH4Y'><ins id='rNH4Y'></ins><ul id='rNH4Y'></ul><sub id='rNH4Y'></sub></form><legend id='rNH4Y'></legend><bdo id='rNH4Y'><pre id='rNH4Y'><center id='rNH4Y'></center></pre></bdo></b><th id='rNH4Y'></th></span></q></dt></tr></i><div id='rNH4Y'><tfoot id='rNH4Y'></tfoot><dl id='rNH4Y'><fieldset id='rNH4Y'></fieldset></dl></div>

                2. <small id='rNH4Y'></small><noframes id='rNH4Y'>

                    <bdo id='rNH4Y'></bdo><ul id='rNH4Y'></ul>

                  <legend id='rNH4Y'><style id='rNH4Y'><dir id='rNH4Y'><q id='rNH4Y'></q></dir></style></legend>