• <small id='UcPUD'></small><noframes id='UcPUD'>

      <bdo id='UcPUD'></bdo><ul id='UcPUD'></ul>
    <tfoot id='UcPUD'></tfoot>

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

        Kivy:使用 on_press 事件在屏幕管理器中更改屏幕

        时间:2023-10-10

        <tfoot id='JxBF2'></tfoot>

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

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

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

            <tbody id='JxBF2'></tbody>
                • <bdo id='JxBF2'></bdo><ul id='JxBF2'></ul>
                  本文介绍了Kivy:使用 on_press 事件在屏幕管理器中更改屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想知道如何使用绑定到按钮的 on_press 事件来更改屏幕,而不使用 KV 文件/KV 语言.

                  I would like to know how to change screens using an on_press event binded to a button, without using a KV file/KV language.

                  我已通读 Kivy 文档,但只能使用 KV 文件找到解决方案.

                  I have read through the Kivy documentation, but have only been able to find solutions using a KV file.

                  例子:

                  on_press: root.manager.current = 'screen2'

                  我还可以使用以下方法更改主 python 文件中的屏幕:

                  I can also change the screen in the main python file using:

                  screenmanager.current = 'screen2'

                  但我不知道如何使用按钮来达到同样的效果.

                  But I cant figure out how to achieve the same using a button.

                  推荐答案

                  实现此目的的一种简单方法是定义自己的按钮子类:

                  One simple way to accomplish this is to define your own button subclass:

                  class ScreenButton(Button):
                      screenmanager = ObjectProperty()
                      def on_press(self, *args):
                          super(ScreenButton, self).on_press(*args)
                          self.screenmanager.current = 'whatever'
                  

                  按下按钮时会自动调用on_press方法,所以会改变screenmanager的current属性.

                  The on_press method is automatically called when the button is pressed, so the screenmanager's current property will be changed.

                  然后你可以有类似的代码:

                  Then you can have code something like:

                  sm = ScreenManager()
                  
                  sc1 = Screen(name='firstscreen')
                  sc1.add_widget(ScreenButton(screenmanager=sm))
                  
                  sc2 = Screen(name='whatever')
                  sc2.add_widget(Label(text='another screen'))
                  
                  sm.add_widget(sc1)
                  sm.add_widget(sc2)
                  

                  单击按钮应根据需要切换屏幕.

                  Clicking the button should switch the screens as required.

                  另一种方式(这可能是 kv 语言实际的做法)是手动使用 bind 方法.

                  Another way (which is probably how kv language actually does it) would be to manually use the bind method.

                  def switching_function(*args):
                      some_screen_manager.current = 'whatever'
                  
                  some_button.bind(on_press=switching_function)
                  

                  这意味着只要按下 some_button 就会调用 switching_function.当然,关于如何以及何时定义函数,这里有很大的灵活性,因此(例如)您可以做一些更一般的事情,比如将屏幕管理器作为第一个参数传递给函数.

                  This would mean that switching_function is called whenever some_button is pressed. Of course there is a lot of flexibility here regarding how and when you define the function, so (for instance) you could do something more general like pass the screenmanager as the first argument to the function.

                  我没有测试这段代码,它不是一个完整的应用程序,但希望含义清楚.任何一种方法都应该可以正常工作,您可以选择看起来最明智的方法.稍后我可能会构建一个更完整的示例.

                  I didn't test this code and it isn't a complete app, but hopefully the meaning is clear. Either method should work fine, you can choose the way that seems most sensible. I might construct a more complete example later.

                  这篇关于Kivy:使用 on_press 事件在屏幕管理器中更改屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:SDL2 错误:“无法加载图像 &lt;default.png&gt; 下一篇:从另一个线程更改 kivy 属性

                  相关文章

                  最新文章

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

                  <tfoot id='CiAiB'></tfoot>

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