我是新的python,我試圖在一個類中創建一個對象,然後從另一個類中刪除同一個對象。從python的另一個類創建一個類對象
這是代碼的一部分:
class Win(QMainWindow):
list_1 = [] #This lists are filled with objects
list_2 = []
def __init__(self):
#A lot of stuff in here
self.splitter = QSplitter(Qt.Vertical)
def addObject(self):
plot = Matplotlib() #This is another class where i create a matplotlib figure
if len(Win.list_1) < 2:
self.splitter.addWidget(plot)
所以,用這一點,我創建一個對象,如果在LIST_1項的數目是低於3,並且然後我將它添加到LIST_1和list_2,和當然,我將它添加到分離器中。這工作正常。現在,我創造這樣刪除這個分路器(與它裏面太的對象)的方法:
deleteObject(self):
if len(Win.list_1) == 1:
widget_erased = self.splitter.widget(index)
widget_erased.hide()
widget_erased.deleteLater()
正如你所看到的,如果我有1個對象,我可以將其刪除。問題出現時,我有更多的對象。在這個同樣的方法我寫:
if len(Win.list_1) > 1:
#I open A QDialog where i see the names of the objects from the lists in a QListWidget
self.delete = Delete()
self.delete.exec_()
現在,這是與QDialog的類:
class Delete(self):
def _init__(self):
#A lot of stuff in here
def deleteObjectCreated(self):
#There are another things before the next lines
widget_erased = Win.splitter.widget(index)
widget_erased.hide()
widget_erased.deleteLater()
利用這最後的方法,我選擇在QDialog的對象,當我按下一個按鈕,對象從兩份名單中刪除,但仍然分路器,我得到這個錯誤:
type object "Win" has no attribute "splitter"
我怎樣才能做到這一點?我的意思是,刪除我從QDialog中選擇的對象,這是在另一個類中創建的?
希望你能幫助我。
非常感謝你的回答,這對我有用。 –