2017-10-16 116 views
1

因此,我的想法是,我应该能够启动脚本,选择屏幕的一部分,然后按回车(或以其他方式触发它)以保存选择。PyQt5抓取和保存屏幕部分

我已经从其他帖子和事物中获得了很多代码,但现在我被卡住了。我可以选择屏幕的任何部分并根据需要调整大小,但似乎无法使其识别“输入”键。现在“keyPressEvent”函数只是打印一条消息,所以我知道它的工作,但我什么都没有。有任何想法吗?

import sys 
from PyQt5 import QtGui, QtCore, QtWidgets 
from PyQt5.QtCore import Qt, QPoint, QRect, QSize 
from PyQt5.QtGui import QScreen 
from PyQt5.QtWidgets import QApplication, QLabel, QRubberBand 


class MyLabel(QtWidgets.QLabel): 

    def __init__(self, parent=None): 
     QtWidgets.QLabel.__init__(self, parent) 
     self.selection = QtWidgets.QRubberBand(QtWidgets.QRubberBand.Rectangle, self) 


    def keyPressEvent(self, qKeyEvent): 
     print(qKeyEvent.key()) 
     if qKeyEvent.key() == QtCore.Qt.Key_Return: 
      print('Enter pressed') 
     else: 
      super().keyPressEvent(qKeyEvent) 

    def mousePressEvent(self, event): 
     ''' 
      Mouse is pressed. If selection is visible either set dragging mode (if close to border) or hide selection. 
      If selection is not visible make it visible and start at this point. 
     ''' 

     if event.button() == QtCore.Qt.LeftButton: 

      position = QtCore.QPoint(event.pos()) 
      if self.selection.isVisible(): 
       # visible selection 
       if (self.upper_left - position).manhattanLength() < 20: 
        # close to upper left corner, drag it 
        self.mode = "drag_upper_left" 
       elif (self.lower_right - position).manhattanLength() < 20: 
        # close to lower right corner, drag it 
        self.mode = "drag_lower_right" 
       else: 
        # clicked somewhere else, hide selection 
        self.selection.hide() 
      else: 
       # no visible selection, start new selection 
       self.upper_left = position 
       self.lower_right = position 
       self.mode = "drag_lower_right" 
       self.selection.show() 

    def mouseMoveEvent(self, event): 
     ''' 
      Mouse moved. If selection is visible, drag it according to drag mode. 
     ''' 

     if self.selection.isVisible(): 
      # visible selection 
      if self.mode is "drag_lower_right": 
       self.lower_right = QtCore.QPoint(event.pos()) 
      elif self.mode is "drag_upper_left": 
       self.upper_left = QtCore.QPoint(event.pos()) 
      # update geometry 
      self.selection.setGeometry(QtCore.QRect(self.upper_left, self.lower_right).normalized()) 


class mainUI(QtWidgets.QWidget): 

    def __init__(self): 
     super(mainUI, self).__init__() 
     self.initUI() 

    def initUI(self): 
     layout = QtWidgets.QVBoxLayout(self) 

     label = MyLabel(self) 
     pixmap = QScreen.grabWindow(app.primaryScreen(), app.desktop().winId()) 
     label.setPixmap(pixmap) 
     layout.addWidget(label) 

     self.setLayout(layout) 

     geometry = app.desktop().availableGeometry() 

     self.setFixedSize(geometry.width(), geometry.height()) 

     # self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint) 
     self.show() 


if __name__ == '__main__': 
    app = QtWidgets.QApplication(sys.argv) 

    window = mainUI() 

    sys.exit(app.exec_()) 

编辑

好吧此代码的工作,但说实话,我不知道为什么100%。

import sys 
from PyQt5 import QtGui, QtCore, QtWidgets 
from PyQt5.QtCore import Qt, QPoint, QRect, QSize 
from PyQt5.QtGui import QScreen 
from PyQt5.QtWidgets import QApplication, QLabel, QRubberBand, QAction 

class KpeWindow(QtWidgets.QLabel): 
    def __init__(self, parent=None): 
     QtWidgets.QLabel.__init__(self,parent) 
     main = QtWidgets.QVBoxLayout(self) 
     self.selection = QtWidgets.QRubberBand(QtWidgets.QRubberBand.Rectangle, self) 

     # label = QLabel(self) 
     # label.setText('Test the keyPressEvent') 
     # main.addWidget(label) 

     # self.adjustSize() 
     # self.setLayout(main) 

    def keyPressEvent(self, event): 
     print(event.key()) 
     if event.key() == QtCore.Qt.Key_Return: 
      print('yay') 

     #QtWidgets.QMessageBox.warning(self, 'MDI', 'keyPressEvent') 
     self.parent().keyPressEvent(event) 


    def mousePressEvent(self, event): 
     ''' 
      Mouse is pressed. If selection is visible either set dragging mode (if close to border) or hide selection. 
      If selection is not visible make it visible and start at this point. 
     ''' 
     print(event) 
     if event.button() == QtCore.Qt.LeftButton: 

      position = QtCore.QPoint(event.pos()) 
      if self.selection.isVisible(): 
       # visible selection 
       if (self.upper_left - position).manhattanLength() < 20: 
        # close to upper left corner, drag it 
        self.mode = "drag_upper_left" 
       elif (self.lower_right - position).manhattanLength() < 20: 
        # close to lower right corner, drag it 
        self.mode = "drag_lower_right" 
       else: 
        # clicked somewhere else, hide selection 
        self.selection.hide() 
      else: 
       # no visible selection, start new selection 
       self.upper_left = position 
       self.lower_right = position 
       self.mode = "drag_lower_right" 
       self.selection.show() 

    def mouseMoveEvent(self, event): 
     ''' 
      Mouse moved. If selection is visible, drag it according to drag mode. 
     ''' 

     if self.selection.isVisible(): 
      # visible selection 
      if self.mode is "drag_lower_right": 
       self.lower_right = QtCore.QPoint(event.pos()) 
      elif self.mode is "drag_upper_left": 
       self.upper_left = QtCore.QPoint(event.pos()) 
      # update geometry 
      self.selection.setGeometry(QtCore.QRect(self.upper_left, self.lower_right).normalized()) 

class MainWindow(QtWidgets.QWidget): 
    def __init__(self): 
     super(MainWindow, self).__init__() 
     QtWidgets.QMainWindow.__init__(self) 
     #self.setWindowTitle('KeyPressEvent Test') 
     # main = QtWidgets.QVBoxLayout(self) 
     # child = KpeWindow(self) 
     # child.setFocusPolicy(Qt.StrongFocus) 
     # self.setFocusProxy(child) 
     # main.addWidget(child) 
     # child.setFocus(True) 

     layout = QtWidgets.QVBoxLayout(self) 

     label = KpeWindow(self) 
     pixmap = QScreen.grabWindow(app.primaryScreen(), app.desktop().winId()) 
     label.setPixmap(pixmap) 
     layout.addWidget(label) 
     #new 
     label.setFocusPolicy(Qt.StrongFocus) 
     self.setFocusProxy(label) 
     label.setFocus(True) 

     self.setLayout(layout) 

     geometry = app.desktop().availableGeometry() 

     self.setFixedSize(geometry.width(), geometry.height()) 


     # self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint) 
     self.show() 

if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    mainWin = MainWindow() 
    mainWin.show() 
    sys.exit(app.exec_()) 

回答

1

您需要set the focus policy标签上获得键盘事件:

class MyLabel(QtWidgets.QLabel): 

    def __init__(self, parent=None): 
     ... 
     self.setFocusPolicy(Qt.TabFocus) 
+0

感谢您的回答,但似乎仍然没有被打印“耶”当我按下回车键。还有什么我不了解的吗? – SuperStew

+0

@SuperStew。它在linux上适用于我。你在哪个平台上? – ekhumoro

+0

啊,我在Windows 7上。这是窗户需要时髦工作的情况之一吗? – SuperStew