• <bdo id='W025Z'></bdo><ul id='W025Z'></ul>
      1. <legend id='W025Z'><style id='W025Z'><dir id='W025Z'><q id='W025Z'></q></dir></style></legend>
        <tfoot id='W025Z'></tfoot>

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

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

        单击 Kivy Python 时返回按钮的 ID

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

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

                • <legend id='WCYKX'><style id='WCYKX'><dir id='WCYKX'><q id='WCYKX'></q></dir></style></legend><tfoot id='WCYKX'></tfoot>

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

                  本文介绍了单击 Kivy Python 时返回按钮的 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我遇到的简单问题:

                  当我在我的 Kivy/Python 应用程序中单击一个按钮时,我想要访问该按钮的 ID.

                  # Kivy code
                  
                  Button:
                      id: position_1
                      on_press: app.access_button_id()
                  

                  我进行了各种尝试和错误尝试,但我无法弄清楚.该按钮嵌套如下(作为更大应用程序的一部分),如果这是我的问题的一个因素:

                  I have had various trial and error attempts but I cannot figure it out. The button is nested as follows (as part of a bigger app), if this is a factor in my problem:

                  FloatLayout:
                      AnchorLayout:
                          ScreenManager:
                              Screen:
                                  BoxLayout:
                                      FloatLayout:
                                          Button:
                                              id: position_1
                                              on_press: app.access_button_id()
                  

                  这是我的 Python 代码,它返回所有的 id.这和我得到的一样接近:

                  Here is my Python code, which returns all of the id's. That's as close as I have got:

                  # Python code
                  
                  def access_button_id(name):
                      print(self.root.ids)
                  

                  我遇到的问题是我什至不知道我应该在文档中查看什么,因为我还没有完全理解术语,所以我找不到值得学习的正确信息.

                  The problem I've got is that I don't even know what I should be looking at in the documentation as I do not fully understand the terminology yet, so I can't find the right information to learn from.

                  传递给函数的(名称)用于某些其他功能,与此问题无关.

                  The (name) passed into the function is for some other functionality, not related to this problem.

                  推荐答案

                  你已经知道 id - 你自己设置它.这些主要用作代码中的硬编码值:

                  You already know the id - you set it yourself. These are mostly used as a hard-coded values in your code:

                  from kivy.app import App
                  from kivy.uix.boxlayout import BoxLayout
                  from kivy.lang import Builder
                  
                  
                  Builder.load_string('''
                  <MyWidget>:
                      Button:
                          id: id_value_1
                          text: 'Print my id'
                          on_press: print({'id_value_1': self.proxy_ref})
                      Button:
                          id: id_value_2
                          text: 'Print all ids'
                          on_press: print(root.ids)
                  ''')
                  
                  
                  class MyWidget(BoxLayout):
                      pass
                  
                  
                  class MyApp(App):
                      def build(self):
                          widget = MyWidget()
                          print({'id_value_1': widget.ids['id_value_1']})
                          return widget
                  
                  
                  if __name__ == '__main__':
                      MyApp().run()
                  

                  如果您已经可以访问它,为什么还需要 Button 的 id?你想完成什么?

                  Why do you need id of a Button if you already have an access to it? What are you trying to accomplish?

                  编辑

                  评论中提到的问题的示例解决方案:

                  An example solution to problem mentioned in the comment:

                  import random
                  import functools
                  
                  from kivy.app import App
                  from kivy.uix.boxlayout import BoxLayout
                  from kivy.uix.popup import Popup
                  from kivy.uix.button import Button
                  from kivy.lang import Builder
                  
                  
                  Builder.load_string('''
                  <MyWidget>:
                      Button:
                          text: 'B1'
                          on_press: root.open_popup(self)
                      Button:
                          text: 'B2'
                          on_press: root.open_popup(self)
                  ''')
                  
                  class MyWidget(BoxLayout):
                      def open_popup(self, caller):
                          fun = functools.partial(self.rename, caller)
                          popup = Popup(
                              title='Test popup',
                              content=Button(
                                  text='rename',
                                  on_press=fun
                              ),
                              size_hint=(None, None), size=(400, 400)
                          )
                          popup.open()
                  
                      def rename(self, caller, *args):
                          caller.text = ''.join(chr(random.randint(48, 90)) for i in range(5))
                  
                  
                  class MyApp(App):
                      def build(self):
                          return MyWidget()
                  
                  
                  if __name__ == '__main__':
                      MyApp().run()
                  

                  这篇关于单击 Kivy Python 时返回按钮的 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 Kivy 中部署多窗口应用程序 下一篇:kivy.uix.screenmanager.ScreenManagerException:ScreenManager 只

                  相关文章

                  最新文章

                • <legend id='uaPKI'><style id='uaPKI'><dir id='uaPKI'><q id='uaPKI'></q></dir></style></legend>

                      • <bdo id='uaPKI'></bdo><ul id='uaPKI'></ul>
                    1. <tfoot id='uaPKI'></tfoot>

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

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