• <legend id='m3AQk'><style id='m3AQk'><dir id='m3AQk'><q id='m3AQk'></q></dir></style></legend>
      <bdo id='m3AQk'></bdo><ul id='m3AQk'></ul>

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

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

        <tfoot id='m3AQk'></tfoot>
      2. 切换 kivy 小部件

        时间:2023-10-10

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

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

                  <tfoot id='rohNP'></tfoot>
                  本文介绍了切换 kivy 小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 Kivy python 库.

                  I am using the Kivy python library.

                  我定义了两个小部件.

                  程序运行时,我运行第一个小部件.

                  When the program runs, I run the first widget.

                  当那个小部件按钮被按下时,我希望它消失并被第二个小部件替换.

                  When that widgets button is pressed, I want it to dissapear and be replaced with the second widget.

                  这是两个小部件的 .kv

                  Here is the .kv for the two widgets

                  #uitest.kv
                  <TestForm>:
                      canvas:
                          Rectangle:
                              pos: self.center_x, 0
                              size: 10, self.height
                  
                      BoxLayout:
                          size: root.size
                          padding: 40
                          Button:
                              text: 'Hello'
                              on_release: root.testCallback()
                  
                  <TestForm2>:
                      canvas:
                          Rectangle:
                              pos: self.center_x, 0
                              size: self.height, 10
                  

                  我的主要 python 文件运行应用程序,并返回第一个小部件

                  My main python file runs the app, and returns the first widget

                  #main.py
                  from testform import TestForm
                  from kivy.app import App
                  
                  class UITestApp(App):
                      def build(self):
                          return TestForm()
                  
                  # Main launching point
                  if __name__ in ('__main__', '__android__'):
                      UITestApp().run()
                  

                  我的第一个小部件有一个回调.这是有问题的代码所属的地方

                  My first widget has a callback. This is where the code-in-question belongs

                  from testform2 import TestForm2
                  from kivy.uix.widget import Widget
                  
                  class TestForm(Widget):
                      def testCallback(self):
                          TestForm2() # Code in question goes here. @TODO replace this widget with TestForm2 widget.
                  

                  这里的想法是拥有一个用户界面管理器.该管理器不像树那样运行 UI,而是像列表和堆栈一样运行.该列表包含我所有的 UI 表单的实例.堆栈保存所述表单的遍历,每当我们跳转到一个表单时,我们将其推送到堆栈并渲染"或其他任何一个.

                  The idea here is to have a user interface manager. This manager doesn't run the UI like a tree, but like a list and stack. The list holds instances of all my UI Forms. The stack holds the traversal of said forms, whenever we jump to a form we push it to the stack and "render" or whatever that one.

                  我选择了我的答案,但在搜索中我还找到了 Screen 对象:http://kivy.org/docs/api-kivy.uix.screenmanager.html就个人而言,clear() 和 add() 命令更强大,但如果您有兴趣,屏幕会从您手中夺走很多东西.过渡效果也是如此.

                  I chose my answer, but in my searches I also found the Screen object: http://kivy.org/docs/api-kivy.uix.screenmanager.html Personally, the clear() and add() commands are more powerful, but the screen takes a lot of that out of your hands if you're interested. Transition effects too.

                  推荐答案

                  我的建议是有一个界面管理器小部件,然后您可以为您的 UI 表单提供各种小部件.

                  My suggestion is to have an interface manager widget, then you can have various widgets for your UI forms.

                  import kivy
                  from kivy.uix.label import Label
                  from kivy.uix.button import Button
                  from kivy.uix.boxlayout import BoxLayout
                  from kivy.app import App
                  
                  class InterfaceManager(BoxLayout):
                  
                      def __init__(self, **kwargs):
                          super(InterfaceManager, self).__init__(**kwargs)
                  
                          self.first = Button(text="First")
                          self.first.bind(on_press=self.show_second)
                  
                          self.second = Button(text="Second")
                          self.second.bind(on_press=self.show_final)
                  
                          self.final = Label(text="Hello World")
                          self.add_widget(self.first)
                  
                      def show_second(self, button):
                          self.clear_widgets()
                          self.add_widget(self.second)
                  
                      def show_final(self, button):
                          self.clear_widgets()
                          self.add_widget(self.final)
                  
                  
                  class MyApp(App):
                      def build(self):
                          return InterfaceManager(orientation='vertical')
                  
                  if __name__ == '__main__':
                      MyApp().run()
                  

                  当然,你不会那样组织它.您可以将所有表单保存在 Container 对象的字典中,并有一个通用回调,通过键提供另一种表单.

                  You wouldn't structure it like that of course. You could hold all your forms in a dictionary on the Container object and have a universal callback which provides another form by key.

                  class InterfaceManager(BoxLayout):
                  
                      def __init__(self, **kwargs):
                          super(InterfaceManager, self).__init__(**kwargs)
                          self.forms = {}
                  
                      def add_form(self, key, form):
                          self.forms[key] = form
                  
                      def uniCallback(self, button):
                          self.clear_widgets()
                          # You could introduce a more elegant key
                          # handling system here.
                          self.add_widget(self.forms[button.text])
                  

                  这篇关于切换 kivy 小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 Kivy 中将图像对象作为按钮背景传递 下一篇:Kivy 日期选择器小部件

                  相关文章

                  最新文章

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

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

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

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