Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从不同的函数pyqt5调用线程_Python_Pyqt5 - Fatal编程技术网

Python 如何从不同的函数pyqt5调用线程

Python 如何从不同的函数pyqt5调用线程,python,pyqt5,Python,Pyqt5,上面的代码显示了从print_key函数更新文本框时的错误 它显示了这个错误: import sys from PyQt5 import QtCore from PyQt5 import QtWidgets from PyQt5.QtWidgets import QMainWindow, QApplication, QPlainTextEdit, QApplication, QMainWindow, QLabel, QComboBox from PyQt5.QtGui import QPixm

上面的代码显示了从print_key函数更新文本框时的错误 它显示了这个错误:

import sys
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication,  QPlainTextEdit, QApplication, QMainWindow, QLabel, QComboBox
from PyQt5.QtGui import QPixmap
from pynput import keyboard
from pynput.keyboard import Listener, Controller
import pyperclip as pc 

keyboard = Controller()
class App(QMainWindow):
    def __init__(self, parent=None):
        super(App, self).__init__(parent)
        #super().__init__()
        label = QLabel(self)
        pixmap = QPixmap('E:/copycat/new.png')
        label.setPixmap(pixmap)
        label.setGeometry(0,0,900,400) 
        self.title = 'COPYCAT'
        self.left = 10
        self.top = 10
        self.width = 400
        self.height = 140
        self.initUI()
        self.key()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        ###########
        combo = QComboBox(self)
        shotcut_list = ["Key.f9","Key.f2","Key.f3","Key.f4","Key.f5","Key.f6","Key.f7","Key.f8","Key.f1","Key.f10","Key.f11","Key.f12"]
        combo.addItems(shotcut_list)
        global shortcut
        global cptext
        shortcut = combo.currentText()
        combo.setGeometry(350, 120, 120, 30)
        combo.activated[str].connect(self.onChanged)  
        # Create textbox
        self.textbox = QPlainTextEdit(self)
        self.textbox.move(20, 160)
        self.textbox.setReadOnly(True)
        self.textbox.resize(500,205)
        self.setGeometry(70,70,540,388)
        self.show()

    def onChanged(self, text):
        global shortcut
        shortcut=text
        
    def print_key(self,key):
        if str(key) == shortcut:
            cptext = pc.paste() 
            keyboard.type(cptext)
            self.textbox.insertPlainText(cptext)
            self.textbox.insertPlainText("\n")

    def key(self):    
        listener = Listener(on_press=self.print_key)
        listener.start()
    
if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = App()
    #ex.key()
    sys.exit(app.exec_())

与on_press关联的回调在次线程中执行,因此您的实现正在从Qt禁止的次线程更新GUI,相反,您应该使用这些信号,因为它们是线程安全的

导入系统 导入线程 从PyQt5.QtCore导入pyqtSignal、pyqtSlot、QObject 从PyQt5.QtGui导入QPixmap 从PyQt5.QtWidgets导入( QApplication, QComboBox, QLabel, QMainWindow, QPlainTextEdit, ) 从pynput.keyboard导入侦听器、控制器 将pyperclip作为pc导入 类键盘侦听器(QObject): textChanged=pyqtSignal(str) def uuu init uuuu(self,shortcut,parent=None): super()。\uuuu init\uuuu(父级) self.\u快捷方式=快捷方式 listener=listener(按下=self.handle) listener.start() self.mutex=threading.Lock() @财产 def快捷方式(自我): 返回自我 按下def手柄(自动,按键): 使用self.mutex: 如果str(key)=self.shortcut: cptext=pc.paste() self.textChanged.emit(cptext) @pyqtSlot(str) def update_快捷方式(自我,快捷方式): 使用self.mutex: self.\u快捷方式=快捷方式 类应用程序(QMainWindow): def uuu init uuu(self,parent=None): 超级(应用程序,自我)。\uuuuu初始化\uuuuuuuu(父级) #super()。\uuuu init\uuuuu() label=QLabel(自) pixmap=QPixmap(“E:/copycat/new.png”) label.setPixmap(pixmap) label.setGeometry(0,0,900,400) self.title=“模仿” self.left=10 self.top=10 自宽=400 自身高度=140 self.initUI() def initUI(self): self.setWindowTitle(self.title) self.setGeometry(self.left、self.top、self.width、self.height) ########### 组合=QComboBox(自身) shotcut_列表=[ “Key.f9”, “密钥f2”, “密钥f3”, “Key.f4”, “Key.f5”, “Key.f6”, “Key.f7”, “Key.f8”, “Key.f1”, “Key.f10”, “键f11”, “密钥f12”, ] 组合添加项(shotcut_列表) 快捷方式=combo.currentText() 集合几何体(350、120、120、30) self.textbox=QPlainTextEdit(self) self.textbox.move(20160) self.textbox.setReadOnly(True) self.textbox.resize(500205) 自身设置几何(70、70、540、388) self.keyboard=Controller() self.listener=KeyboardListener(combo.currentText()) combo.activated[str].connect(self.listener.update\u快捷方式) self.listener.textChanged.connect(self.handle\u text\u changed) @pyqtSlot(str) def句柄_文本_已更改(自身,文本): self.textbox.insertPlainText(文本) self.textbox.insertPlainText(“\n”) self.keyboard.type(文本) def main(): app=QApplication(sys.argv) ex=App() 例如:show() sys.exit(app.exec_()) 如果名称=“\uuuuu main\uuuuuuuu”: main()
不允许从外部线程访问UI元素。在任何情况下,为什么要使用pynput呢?非常感谢,先生,您能帮助我知道从哪里开始使用python和pyqt5吗
Cannot queue arguments of type 'QTextBlock'
(Make sure 'QTextBlock' is registered using qRegisterMetaType()
Cannot queue arguments of type 'QTextCursor'
(Make sure 'QTextCursor' is registered using qRegisterMetaType()