![](/img/trans.png)
[英]How do I use binary modules in Kivy or BeeWare to create an iOS app?
[英]Use own modules in beeware
我有一個 beeware 項目,也想在其中使用我自己的模塊,例如模型和控制器。 另外,一個模塊創建了一些我可以測試的對象。
但是當我想導入模塊來創建測試對象並使用它只是拋出一個錯誤的方法:
ImportError: attempted relative import beyond top-level package
經過一番研究,我知道路徑(目錄)結構、我的模塊所在的位置以及 package 的位置很重要。 但是無論我把模塊放在哪里,它都有相同(或有點像這樣)的錯誤。 但是我可以導入我的模型來創建這些類的對象。 我也無法確定公文包的起點在哪里。
這是我目前的結構:
/Project_Dir (own created)
/briefcase_project (created from briefcase)
/src
/Models (own created)
/app_directory (created from briefcase)
here is the __main__.py and the __init__.py (the start point I guess) and the app.py (where beeware code is, and also my module import from Test)
/Test (own created, here is a file with a method I want to call)
可悲的是,沒有太多關於beeware的東西可以找到,所以我可以找到解決方案。
請幫忙。 謝謝^^
我做了以下工作來解決這個問題。 使用Beeware 教程 2源代碼的示例位於Github
.
├── __init__.py
├── __main__.py
├── app.py
├── mylib <--- # my lib.
│ ├── __init__.py
│ └── testlib.py
└── resources
├── __init__.py
├── beewarecustomlibexample.icns
├── beewarecustomlibexample.ico
└── beewarecustomlibexample.png
2 directories, 9 files
mylib/testlib.py
def test(text: str) -> str:
return f"Hello: {text}"
在app.py
:
import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW
from beewarecustomlibexample.mylib.testlib import test # Import custom lib
class BeewareCustomLibExample(toga.App):
def startup(self):
...
def say_hello(self, widget):
# Calling my test method
result = test(self.name_input.value)
self.main_window.info_dialog("Test Dialog", result)
def main():
return BeewareCustomLibExample()
以上是我如何讓它工作的。 我已經在 MacOS 上構建了它並且工作正常。
取你的項目文件夾名稱,然后從那里導入,所以如果你正在修改教程,並且你已經在 app.py 的同一目錄中設置了一個名為myModule
的模塊文件夾,並且你有一個名為app.py
的file.py
一個名為myClass
的 class ,您可以鍵入:
from helloworld.myModule.file import myClass
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.