我正在使用 Kivy python 库。
我定义了两个小部件。
当程序运行时,我运行第一个小部件。
当按下该小部件按钮时,我希望它消失并被第二个小部件替换。
这是两个小部件的 .kv
#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 文件运行应用程序,并返回第一个小部件
#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()
我的第一个小部件有一个回调。这是有问题的代码所属的位置
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 表单的实例。堆栈保存所述表单的遍历,每当我们跳转到表单时,我们将其推入堆栈并“渲染”或其他任何形式。
编辑:
我选择了我的答案,但在搜索中我还找到了 Screen 对象:http://kivy.org/docs/api-kivy.uix.screenmanager.html http://kivy.org/docs/api-kivy.uix.screenmanager.html就个人而言,clear() 和 add() 命令更强大,但如果您感兴趣的话,屏幕会占用您大量的时间。也有过渡效果。