Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
145 views
Welcome To Ask or Share your Answers For Others

1 Answer

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...