我正在嘗試制作一個二維圖形包。我已經做了很多嘗試來尋找最好的結構,但我不能讓它很好地作業。這是當前不作業的設定:
我有檔案
init.py,scene.py并且polygon.py
scene.py 應該初始化一個單一的物件,這個物件有一個陣列,假設多邊形被存盤在其中。這個檔案的一個簡單版本如下所示:
class make_scene:
def __init__(self,width,height,**kwargs):
self.width = width
self.height = height
self.color = kwargs.get('color', '#ffffff')
self.draw_elements = []
#init scene:
the_scene = make_scene(500, 250)
polygon.py 定義一些要繪制的物件(圓形、立方體等):
class cube:
def __init__(self,x,y,**kwargs):
self.x = x
self.y = y
self.color = kwargs.get('color', '#000000')
the_scene.append(self)
init.py 只需匯入模塊和一些幫助包:
from .scene import *
from .polygon import *
但是,如果我嘗試使用此包,則scene_file 的范圍不正確:
cube(0,0,10,10)
>>> NameError: name 'the_scene' is not defined
我一直被困在多種不同的架構中。如果我將它保存在一個看起來不太理想的檔案中,我可以讓它作業。
解決這個問題或嘗試不同的架構的想法?我會喜歡你的意見。
uj5u.com熱心網友回復:
問題似乎是您的polygon.py 檔案不知道該the_scene物件。Python 不會將兩者鏈接到一個像 init.py 這樣的公共檔案中。
嘗試the_scene從您的polygon.py 檔案中匯入該函式,例如
from .polygon import the_scene
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/390125.html
上一篇:在while回圈中遞增物件
