.py和.kv交互中的Kivy类 [英] Kivy class in .py and .kv interaction
问题描述
我没有完全掌握python文件中的类与奇异语言的交互方式.我目前正在修改Kivy的Showcase示例,以加深我的理解.
I'm not fully grasping how classes in a python file and the kivy language interact. I'm currently modifying the Showcase example of Kivy to increase my understanding.
- main.py
- showcase.kv
- data/screens/test.kv
class Testy(BoxLayout):
ttext = 'Bla'
#other code
class ShowcaseScreen(Screen):
fullscreen = BooleanProperty(False)
def add_widget(self, *args):
if 'content' in self.ids:
return self.ids.content.add_widget(*args)
return super(ShowcaseScreen, self).add_widget(*args)
class ShowcaseApp(App):
#code
test.kv
<Testy>:
orientation: 'vertical'
Label:
text: root.ttext
ShowcaseScreen:
name: 'LearnKanji'
fullscreen: True
#Some ref to <Testy>
问题
我想将代码包含在Testy()中,因为我打算编写大量针对单个屏幕中发生的事情的代码,并且我不想弄乱ShowcaseScreen(),因为ShowcaseScreen也意味着用于其他屏幕.
Question
I want to have my code in Testy(), because I'm planning to write a lot of code aimed at what happens in that single screen and I don't want to clutter ShowcaseScreen(), because ShowcaseScreen is also meant for other screens.
为澄清起见,在test.kv中,我指的是变量ttext.如果我不创建Testy()类,则将其放置如下:
To clarify, in test.kv I'm referring to the variable ttext. If I didn't make the class Testy() I would have put it as follows:
ShowcaseScreen:
name: 'LearnKanji'
fullscreen: True
BoxLayout:
text: root.ttext
然后在main.py中,我需要放
And then in main.py I would need to put
class ShowcaseScreen(Screen):
ttext = 'Bla'
但是,由于除Testy之外的许多屏幕都使用ShowcaseScreen()类,因此将所有屏幕的所有代码都放在此处会使情况变得一团糟.因此,我希望每个屏幕的代码都在单独的类中.我认为这对于大型代码项目是一种更好的方法.
However since many screens besides Testy use the class ShowcaseScreen(), putting all the code for all screens in here would make it too much of a mess. Therefore I want the code per screen in a separate class. I think this is a better way for bigger code projects.
那么在test.kv文件中调用ShowcaseScreen:之后,我该如何指代?这样我就可以将Testy专用的代码放在Testy()中,并使文件和类更井井有条.
So how do I refer to after calling ShowcaseScreen: in the test.kv file? So that I can put the code meant for Testy in Testy() and keep my files and classes more organized.
推荐答案
您可以使用ScreenManager
.在这里看看,我更改了一些代码,以提供使用ScreenManager将每个屏幕的代码分成各自的类的示例.如果有任何问题,请通知我.
You can use the ScreenManager
. Have a look here, I changed some of your code to provide an example of using the ScreenManager to seperate out the code for each screen into their own classes. If there are problems with this let me know.
main.py :
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.properties import ObjectProperty
class Testy(Screen):
ttext = 'Screen 2'
#other code
class ScreenTwo(Screen):
pass
class Manager(ScreenManager):
testy = ObjectProperty(None)
screen_2 = ObjectProperty(None)
class ShowcaseApp(App):
def build(self):
return Manager(transition=FadeTransition())
if __name__ == "__main__":
ShowcaseApp().run()
kv文件:
<Testy>:
BoxLayout:
Button:
text: root.ttext
on_press: root.manager.current = "screen_two"
<ScreenTwo>:
BoxLayout:
Button:
text: "Screen 1"
on_press: root.manager.current = "testy_screen"
<Manager>:
id: screen_manager
testy: testy
screen_2: screen_2
Testy:
id: testy
name: 'testy_screen'
manager: screen_manager
ScreenTwo:
id: screen_2
name: 'screen_two'
manager: screen_manager
这篇关于.py和.kv交互中的Kivy类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!