Python 导入和“对象没有属性”与Qt

发布于 2024-08-21 06:56:11 字数 1786 浏览 5 评论 0原文

通过对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁拥 2024-08-28 06:56:12

尝试

self.frameRect().setTopLeft(Qt.QPoint(event.x, event.y))

代替

self.frameRect.setTopLeft(Qt.QPoint(event.x, event.y))

Try

self.frameRect().setTopLeft(Qt.QPoint(event.x, event.y))

instead of

self.frameRect.setTopLeft(Qt.QPoint(event.x, event.y))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文