2017-10-16 161 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