gpt4 book ai didi

python - 寻找组织大型 kivy 项目的好方法

转载 作者:行者123 更新时间:2023-12-05 07:58:28 25 4
gpt4 key购买 nike

我是 kivy 的新手,我想知道如何在单独的目录中使用应用程序的某些部分。我想轻松地重用代码并在项目中有良好的组织。我不喜欢像许多教程中那样将所有东西都放入一个模块中,所以也许有什么方法可以将小部件放入模块中。例如:

-main:
-modules/
-login/
-main.py
-main.kv
-other/
-ohter.kv
-other.kv
-main.py #<- here I want to put widgets from 'login' and 'other'
-main.kv

最佳答案

我通常将每个小部件完全放入一个文件中:

from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder

Builder.load_string('''
<LoginMain>:
Label:
text: 'hi'
Button:
text: 'press me'
''')

class LoginMain(BoxLayout):
...

然后你只需要使用 Factory 告诉 Kivy 在哪里可以找到每个小部件。 .这在您应用的构建方法中最容易做到:

class MyApp(App):
def build(self):
Factory.register('LoginMain', module='myapp.modules.login.main')
Factory.register('Other', module='myapp.modules.other.other')
return Factory.LoginMain()

关于python - 寻找组织大型 kivy 项目的好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24439412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com