python - PyQt 在处理事件时避免陷入无限循环

标签 python python-3.x windows pyqt pyqt5

在 PyQt 中,我试图在每次更改时获取剪贴板的内容,执行一些操作以将输出计算为文本并将其再次放入剪贴板中。

问题似乎是,当我在“剪贴板内容已更改”事件处理函数中更改剪贴板的内容时,这会触发并一遍又一遍地无限调用该函数。

这是我正在使用的代码:

import sys
from PyQt5.Qt import QApplication, QClipboard
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QPlainTextEdit
from PyQt5.QtCore import QSize

class ExampleWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setMinimumSize(QSize(440, 240))
        self.setWindowTitle("PyQt5 Clipboard example")

        # Add text field
        self.b = QPlainTextEdit(self)
        self.b.insertPlainText("Use your mouse to copy text to the clipboard.\nText can be copied from any application.\n")
        self.b.move(10,10)
        self.b.resize(400,200)

        # clipboard
        self.cb = QApplication.clipboard()
        self.cb_changed_handler = self.cb.dataChanged.connect(self.clipboardChanged)

    def clipboardChanged(self):
        text = self.cb.text()
        print(text)
        self.b.insertPlainText(text + '\n')
        self.cb.clear(mode=self.cb.Clipboard)
        self.cb.setText("this is a text", mode=self.cb.Clipboard) # this creates an infinite loop!!    

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWin = ExampleWindow()
    mainWin.show()
    sys.exit( app.exec_() )

我还尝试了以下clipboardChanged函数但没有成功,python崩溃了:

def clipboardChanged(self):
    text = self.cb.text()
    print(text)
    self.b.insertPlainText(text + '\n')
    # This does not work, python crashes!!
    # let's disable the event listener, do what we should and restore the event
    self.cb.dataChanged().disconnect()
    self.cb.clear(mode=self.cb.Clipboard)
    self.cb.setText("this is a text", mode=self.cb.Clipboard)
    self.cb_changed_handler = self.cb.dataChanged.connect(self.clipboardChanged)

我使用的是 Windows。

执行后

from PyQt5.Qt import PYQT_VERSION_STR
print("PyQt version:", PYQT_VERSION_STR)

我明白

PyQt version: 5.9.2

最佳答案

解决办法是阻止QClipBoard dataChanged使用blockSignals()在clipboardChanged方法中发出信号:

def clipboardChanged(self):
    text = self.cb.text()
    print(text)
    self.b.insertPlainText(text + '\n')
    <b>self.cb.blockSignals(True)</b>
    self.cb.clear(mode=self.cb.Clipboard)
    self.cb.setText("this is a text", mode=QClipboard.Clipboard)   
    <b>self.cb.blockSignals(False)</b>

关于python - PyQt 在处理事件时避免陷入无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58182546/

相关文章:

python - 结构重新启动我的 supervisord 服务终止服务

python - python中的for循环连接两列数据

python - 在不使用 for 循环的情况下从列表中访问类变量?

python-3.x - 使用python3的简单nltk情感分析代码

windows - 无法添加对 'Newtonsoft.Json' 的引用

python - 无法导入名称 "Contact"

Python 谷歌 API 构建

python - 在递归函数中附加一个带有列表的空列表

c++ - 自动关闭 MessageBox

windows - 如何从 Windows Phone 8.1 中的代码中关闭 MessageDialog