Python 导入和“对象没有属性”与Qt
通过对 Stack Overflow 和其他网站的研究,我 99% 确信我遇到的问题是由于导入不正确造成的。下面是我用来响应某些鼠标事件的 QLabel 子类:
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
它给出了相同的错误
更新:根据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
(即 self().something()
)
From research on Stack Overflow and other sites I'm 99% sure that the problem I'm having is due to incorrect importing. Below is a QLabel sub class that I'm using to respond to some mouse events:
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))
When mouseMoveEvent is triggered I get the following error:
self.frameRect.setTopLeft(Qt.QPoint(event.x, event.y))
AttributeError: 'builtin_function_or_method' object has no attribute 'setTopLeft'
The other solutions to this type of error I've seen have revolved around the name space, so I would or would not need to include Qt.
before all the Qt classes but this error is much farther down in the Qt objects. Please point out my mistake!
I have also tried:
from PyQt4 import Qt
It gives the same error
UPDATE: based on Messa's comment I made few changes:
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()))
So it seems that in Python the dot syntax are function calls and need to include that trailing "()
". This doesn't include self
( i.e. self().something()
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
代替
Try
instead of