我有兩個文件,分別是game.py和map.py.下面簡單的代碼駐留在其中:使用python靜態類C#/ Java風格,範圍差異錯誤
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from datetime import *
from map import *
class Game:
Scene = QGraphicsScene
View = QGraphicsView
@staticmethod
def start():
Game.App = QApplication(sys.argv)
Game.Scene=QGraphicsScene();
Game.Scene.setBackgroundBrush(QBrush(QColor(Qt.green)));
Game.View=QGraphicsView(Game.Scene);
print Game
map=Map()
Game.App.exec_()
if __name__=='__main__':
Game.start()
和文件map.py:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from game import *
class Map(QObject):
def __init__(self,parent=None):
super(Map,self).__init__(parent);
print Game
v=Game.View
print v
v.setWindowTitle("sth")
運行game.py結果
__main__.Game
game.Game
<class 'PyQt4.QtGui.QGraphicsView'>
Traceback (most recent call last):
File "/home/abiusx/workspace/borj/trunk/game.py", line 119, in <module>
Game.start()
File "/home/abiusx/workspace/borj/trunk/game.py", line 66, in start
map=Map()
File "/home/abiusx/workspace/borj/trunk/map.py", line 16, in __init__
v.setWindowTitle("asb")
TypeError: QWidget.setWindowTitle(QString): first argument of unbound method must have type 'QWidget'
的問題是,在地圖中的代碼。 py引用Game.View,將其檢測爲一個類(如類體中定義的),但不覆蓋它(實際實例)上的內容。 另外遊戲被稱爲主要。遊戲在game.py,但作爲game.Game在map.py
任何人都可以幫助我嗎?
問題在於循環引用。通過更改依賴關係樹來解決它 – AbiusX 2012-04-26 17:48:24