-1

When I am using other programs (e.g. opening a pdf or word), I will select some text contents (like a word or paragraph) by using the mouse. I want my python program to get this text content. How to do this using PyQt, or some other Python library?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
mahome
  • 9
  • 1

1 Answers1

1

This is an easy task, you haven't specified the pyqt version, so I'll post the solution for PyQt4, here you go:

from PyQt4.QtCore import QObject, pyqtSlot, SIGNAL, SLOT
from PyQt4.QtGui import QApplication, QMessageBox
import sys


class MyClipboard(QObject):

    @pyqtSlot()
    def changedSlot(self):
        if(QApplication.clipboard().mimeData().hasText()):
            QMessageBox.information(None, "Text has been copied somewhere!",
                                    QApplication.clipboard().text())


def main():
    app = QApplication(sys.argv)
    listener = MyClipboard()

    app.setQuitOnLastWindowClosed(False)
    QObject.connect(QApplication.clipboard(), SIGNAL(
        "dataChanged()"), listener, SLOT("changedSlot()"))

    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
BPL
  • 9,632
  • 9
  • 59
  • 117
  • Thx for your help, I have run your code in my pc. I just want to select word rather than select word and copy that(ctrl + v). So, I think use system clipboard is not a good solution. – mahome Aug 16 '16 at 08:07