我想在kv文件中创建ScreenManager,但我还需要更改.py文件中显示的屏幕。那是因为我必须创建一些按钮
动态地将特定函数绑定到它们,这将更改为特定(按钮相关)屏幕。在python中创建按钮更方便。所以主要的问题是:如何通过py文件访问在kv文件中创建的screenmanager?
为了进一步解释,这里有一些代码:
kv文件
#: kivy 1.10.1
ScreenManager:
id: screen_manager
FirstScreen:
id: first_screen
name: 'FirstScreen'
manager: 'screen_manager'
SecondScreen:
id: second_screen
name: 'SecondScreen'
manager: 'screen_manager'
py文件
from kivy.modules import console
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.properties import ObjectProperty
class FirstScreen(Screen):
MenuScreen = ObjectProperty(None)
def SwitchToSecond(self):
print(ScreenManagement.current)
ScreenManagement.current = "TestScreen"
class SecondScreen(Screen):
pass
class testApp(App):
pass
if __name__ == "__main__":
testApp().run()
提前感谢您的任何指导
答案 0 :(得分:0)
这是一个使用ScreenManager的简单示例(我还在MyScreenManager类中添加了一个接受// normal, remote dependencies
debugImplementation 'com.remote:dependency1:x.x.x'
qaImplementation 'com.remote:dependency1:x.x.x'
releaseImplementation 'com.remote:dependency1:x.x.x'
debugImplementation 'com.remote:dependency2:x.x.x'
qaImplementation 'com.remote:dependency2:x.x.x'
releaseImplementation 'com.remote:dependency2:x.x.x'
// debugLocal dependencies
debugLocalImplementation project(':dependency1')
debugLocalImplementation project(':dependency2')
的方法,该方法对应于您想要更改的屏幕名称,但不是必需的这个应用程序运行):
<强>的Python 强>
value
<强> KV 强>
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
class Screen1(Screen):
pass
class Screen2(Screen):
pass
class Screen3(Screen):
pass
class MyScreenManager(ScreenManager):
def changescreen(self, value):
self.current = value
#Main application
class TestApp(App):
def build(self):
self.sm = MyScreenManager()
return self.sm
if __name__ == '__main__':
TestApp().run()
答案 1 :(得分:0)
如果要访问屏幕中的ScreenManager
,则必须使用其manager
属性,但为此,您不能创建具有相同名称的属性,在您执行此操作的情况下,被认为是一种不好的做法。
修改代码并添加一些元素我们获得以下示例:
<强> *。KV 强>
#: kivy 1.10.1
ScreenManager:
id: screen_manager
FirstScreen:
id: first_screen
name: 'FirstScreen'
Button:
text: "First"
on_press: first_screen.SwitchToSecond()
SecondScreen:
id: second_screen
name: 'SecondScreen'
Label:
text: "second"
即可。*吡啶强>
from kivy.modules import console
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.properties import ObjectProperty
class FirstScreen(Screen):
MenuScreen = ObjectProperty(None)
def SwitchToSecond(self):
self.manager.current = "SecondScreen"
class SecondScreen(Screen):
pass
class testApp(App):
pass
if __name__ == "__main__":
testApp().run()