0
從堆棧溢出和其他網站的研究我99%確定我遇到的問題是由於不正確的導入。下面是一個QLabel子類,我使用一些鼠標事件作出迴應:Python導入和'對象沒有屬性'與Qt
import Qt
import sys
class ASMovableLabel(Qt.QLabel):
def mouseReleaseEvent(self, event):
button = event.button()
if button == 1:
print ('LEFT CLICK')
def mousePressEvent(self, event):
button = event.button()
if button == 1:
print ('LEFT CLICK')
elif button == 3:
print ('RIGHT CLICK')
self.setLayout()
def mouseMoveEvent(self, event):
print ("you moved the mouse: %f, %f", event.x, event.y)
self.frameRect.setTopLeft(Qt.QPoint(event.x, event.y))
當mouseMoveEvent被觸發,我得到以下錯誤:
self.frameRect.setTopLeft(Qt.QPoint(event.x, event.y))
AttributeError: 'builtin_function_or_method' object has no attribute 'setTopLeft'
這種類型的錯誤的其他解決方案我已經看到了圍繞名字空間,所以我會或不會需要在所有的Qt類之前包含Qt.
,但是這個錯誤在Qt對象中更遠了。請指出我的錯誤!
我也曾嘗試:
from PyQt4 import Qt
它給出了同樣的錯誤
UPDATE:基於MESSA的評論我做了一些改動:
import Qt
import sys
class ASMovableLabel(Qt.QLabel):
def mouseReleaseEvent(self, event):
button = event.button()
if button == 1:
print ('LEFT CLICK')
def mousePressEvent(self, event):
button = event.button()
if button == 1:
print ('LEFT CLICK')
elif button == 3:
print ('RIGHT CLICK')
self.setLayout() #this won't set to nil
def mouseMoveEvent(self, event):
self.frameRect().setTopLeft(Qt.QPoint(event.globalX(), event.globalY()))
如此看來,在Python中點語法是函數調用,需要包含尾隨「()
」。這不包括self
(,就是s elf().something()
)
廣告 「Python中的點語法是函數調用」 - 不,它只是PyQt的是遵循C++習慣而不是Python習慣。當然,如果我創建自己的具有frameRect屬性的類,我可以只寫'self.frameRect.setTopLeft(...)'。在Python中,我甚至可以爲屬性編寫getters/setter(通過'property'裝飾器),我不知道爲什麼它們不在PyQt中使用。 – Messa 2010-02-12 20:54:33