1
調用一個函數我有一個Python程序(使用PyQt4的)的大綱如下:如何從不同的類在Python
class Polygon(QtGui.QGraphicsItem):
def __init__(self):
super(Polygon, self).__init__()
def addpoint(self, point):
if last_point:
# trying to add auto-save here
class MainWidget(QtGui.QWidget):
openFileName = ""
list_of_polygons = []
def __init__(self):
super(MainWidget, self).__init__()
def openFile(self):
call dialog
self.openFileName = ...
def saveFile(self):
# needs to access a couple something with self.variables, like self.openFileName
def main():
app = QtGui.QApplication(sys.argv)
ex = MainWidget()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
功能就是我創建的多邊形標記物體的圖像瀏覽器。創建多邊形後,我想調用自動保存。
因此,爲了保存多邊形,我需要從saveFile
函數MainWidget
類中調用它。我的問題是保存功能是在MainWidget類中實現的,我不知道如何從Polygon
類中訪問它們。
這樣做最好的想法是什麼?我應該讓saveFile成爲全球?如果是,那我該如何進入自我。 MainWidget的變量?
說真的,你正考慮在每個'addpoint()'之後不得不執行'saveFile()'?聽起來很慢,特別是如果用戶正在交互創建點... – martineau
每個多邊形都有4-10點。一旦多邊形完成,我只能保存。只是沒有包含在代碼中。 – zsero