我正在使用 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 就个人而言,clear() 和 add() 命令更多功能强大,但是如果您有兴趣,屏幕会从您手中夺走很多东西。过渡效果也是。