试图仅获得"x".从pos()方法python进行协调 [英] Trying to get just "x" coordinate from pos() method python

查看:108
本文介绍了试图仅获得"x".从pos()方法python进行协调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在获取光标的x位置时遇到问题,因此我可以在x和y位置上放置一个标记.我正在使用QGraphicsScene并在单击鼠标时在鼠标所在的位置查看以创建此圆形对象.由于QGraphicsEllipseItem接受4个参数,因此我似乎需要将x和y坐标分开,而不仅仅是位置方法给您的坐标分开,因为它同时给出了x和y.如何拆分两个坐标?这是代码:

I'm currently having an issue getting just the x position of the cursor so I can place a marker on that x and y location. I'm using QGraphicsScene and view to create this circle object at the location of the mouse when the mouse is clicked. Since the QGraphicsEllipseItem takes 4 arguments it seems I need the x and y coordinate separate not just what the position method gives you since it gives both x and y. How do I split the two coordinates up? Here's the code:

import sys
from PyQt4 import QtGui, QtCore    

def paintMarkers(self):
    self.cursor = QtGui.QCursor()
    self.x,y = self.cursor.pos()
    self.circleItem = QtGui.QGraphicsEllipseItem(self.x,self.y,10,10)
    self.scene.addItem(self.circleItem)
    self.circleItem.setPen(QtGui.QPen(QtCore.Qt.red, 1.5))
    self.setScene(self.scene)

def mousePressEvent(self,QMouseEvent):
    self.view.paintMarkers()

非常感谢!

推荐答案

我不是100%清楚您的问题是什么(您是否遇到异常?它会运行但会出现意外输出吗?),但此行看起来像罪魁祸首:

I'm not 100% clear what your problem is (are you getting an exception? Does it run but you get unexpected output?), but this line looks like the culprit:

self.x,y = self.cursor.pos()

这将创建x作为self的属性,然后创建一个完全不与self关联的局部变量y.如果要将它们都设为self的属性,请执行

This will create x as an attribute of self, and then create a local variable y that has no association with self at all. If you want both of them to be attributes of self, do

self.x, self.y = self.cursor.pos()

如果您在尝试执行QGraphicsEllipseItem(self.x,self.y,10,10)时遇到错误,则可以解释为什么-self.y不存在,因此会出现AttributeError.

If you were getting an error while trying to do QGraphicsEllipseItem(self.x,self.y,10,10), this would explain why - self.y didn't exist, so it would give you an AttributeError.

这篇关于试图仅获得"x".从pos()方法python进行协调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆