我想从图像中选择一个区域并获取她的坐标。我已经搜索并找到了这段代码,它创建了对象橡皮筋,但是我无法调整它的大小..奇怪,在linux(lubuntu)上它可以工作,但在macOS上却没有。.我想要在macOS上使用橡皮筋的版本特别是那些“手柄”,在macOS中这些手柄都不会出现..还有一件事,可以通过右键单击选择并在需要的位置移动图像来移动橡皮筋矩形,例如将图标移动到桌面。谢谢!
import sys
def changeValue(value):
return value + 1
def main(value):
value = changeValue(value)
print('Value is %d' % value)
if __name__ == '__main__':
inputValue = int(sys.argv[1])
while True:
main(inputValue)
答案 0 :(得分:3)
您在正确的轨道上。 PyQt使用事件处理程序来控制鼠标交互和调整事件大小。对于您的应用程序,调整夹点的大小将调用resizeEvent
,而重新绘制更改后的窗口将涉及paintEvent
。类似地,当触发这些操作时,鼠标交互利用mousePressEvent
,mouseMoveEvent
和mouseReleaseEvent
作为事件处理程序的调用。本质上,对于所需的每个交互,必须为该特定事件实现处理程序。我使用PyQt4进行测试,但对于PyQt5应该相同。
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
# from PyQt4 import QtCore, QtWidgets
class ResizableRubberBand(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ResizableRubberBand, self).__init__(parent)
self.draggable = True
self.dragging_threshold = 5
self.mousePressPos = None
self.mouseMovePos = None
self.borderRadius = 5
self.setWindowFlags(QtCore.Qt.SubWindow)
layout = QtWidgets.QHBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(
QtWidgets.QSizeGrip(self), 0,
QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)
layout.addWidget(
QtWidgets.QSizeGrip(self), 0,
QtCore.Qt.AlignRight | QtCore.Qt.AlignBottom)
self._band = QtWidgets.QRubberBand(
QtWidgets.QRubberBand.Rectangle, self)
self._band.show()
self.show()
def resizeEvent(self, event):
self._band.resize(self.size())
def paintEvent(self, event):
# Get current window size
window_size = self.size()
qp = QtWidgets.QPainter()
qp.begin(self)
qp.setRenderHint(QtWidgets.QPainter.Antialiasing, True)
qp.drawRoundedRect(0, 0, window_size.width(), window_size.height(),
self.borderRadius, self.borderRadius)
qp.end()
def mousePressEvent(self, event):
if self.draggable and event.button() == QtCore.Qt.RightButton:
self.mousePressPos = event.globalPos() # global
self.mouseMovePos = event.globalPos() - self.pos() # local
super(ResizableRubberBand, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
if self.draggable and event.buttons() & QtCore.Qt.RightButton:
globalPos = event.globalPos()
moved = globalPos - self.mousePressPos
if moved.manhattanLength() > self.dragging_threshold:
# Move when user drag window more than dragging_threshold
diff = globalPos - self.mouseMovePos
self.move(diff)
self.mouseMovePos = globalPos - self.pos()
super(ResizableRubberBand, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
if self.mousePressPos is not None:
if event.button() == QtCore.Qt.RightButton:
moved = event.globalPos() - self.mousePressPos
if moved.manhattanLength() > self.dragging_threshold:
# Do not call click event or so on
event.ignore()
self.mousePressPos = None
super(ResizableRubberBand, self).mouseReleaseEvent(event)
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.button = QtWidgets.QPushButton('Show Rubber Band')
self.button.clicked.connect(self.handleButton)
self.label = QtWidgets.QLabel()
self.label.setScaledContents(True)
self.label.setPixmap(QtGui.QPixmap('image.JPG'))
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.label)
layout.addWidget(self.button)
def handleButton(self):
self.band = ResizableRubberBand(self.label)
self.band.setGeometry(150, 150, 150, 150)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(800, 100, 600, 500)
window.show()
sys.exit(app.exec_())