我有一个连接到CG服务器(CasparCG)的Python QT应用程序。 QT应用程序触发一个QThread,它使用模块 pynput 侦听热键-并向CasparCG发送命令以针对每个按下的键播放不同的视频文件。
在主GUI中,我可以将视频文件分配给热键列表,然后从子菜单项触发热键侦听线程。
self.actionStart_Hotkeys = QtWidgets.QAction(MainWindow)
self.menuCasparCG.addAction(self.actionStart_Hotkeys)
self.actionStart_Hotkeys.triggered.connect(self.StartHotkeys)
Main应用程序和Ui_Window代码非常长,没有问题-它的功能应该像它应该的那样。视频也可以按预期的方式播放,方法是通过按按键激活热键,但是在播放了一些视频文件后,应用程序的主窗口冻结了-我不确定为什么主GUI在热键之后没有响应输入线程已启动。
到目前为止的代码看起来像这样。
from pynput import keyboard
class HotKeys(QThread):
def __init__(self, parent):
QThread.__init__(self, parent)
self.COMBINATIONS = [
{keyboard.KeyCode(char='0')},
{keyboard.KeyCode(char='1')},
{keyboard.KeyCode(char='2')},
{keyboard.KeyCode(char='3')},
]
self.caspar = None
self.current = set()
self.Connect()
self.Listen()
def exit(self, i):
if not self.caspar == None:
self.caspar.close
sys.exit(i)
def Connect(self):
try:
self.caspar = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.caspar.connect(("127.0.0.1", 5250))
print("Connected to caspar")
except socket.error:
print("CasparCG not running, or incorrect settings.xml")
self.exit(0)
def execute(self, k=None): # k is videofile
movie = bytes("PLAY 1-20 {} \r\n".format(k), 'utf8')
self.caspar.send(movie)
def on_press(self, key):
if any([key in COMBO for COMBO in self.COMBINATIONS]):
self.current.add(key)
if any(all(k in self.current for k in COMBO) for COMBO in self.COMBINATIONS):
self.execute(key)
def on_release(self, key):
if any([key in COMBO for COMBO in self.COMBINATIONS]):
self.current.remove(key)
def Listen(self):
with keyboard.Listener(on_press=self.on_press, on_release=self.on_release) as listener:
listener.join()
我像这样在应用程序的Main类中触发此热键QThread ...
class Main(QMainWindow, Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self) # from Ui_MainWindow class
def StartHotkeys(self):
hotkey_thread = HotKeys(self)
hotkey_thread.start()
和类似的应用程序...
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
gui = Main()
gui.show()
sys.exit(app.exec_())
那Main为什么会冻结?
答案 0 :(得分:2)
QThread
不是线程,它是线程处理程序,如果要在另一个线程中执行任务,则必须使用run()
方法来执行,该方法是唯一的部分。在另一个线程中执行。在您的情况下,Listen()
任务正在阻塞,您可以在构造函数中调用它,而QThread
构造函数在GUI线程中运行,这就是GUI冻结的原因。解决方案是将“连接并监听”方法移至run()
:
class HotKeys(QThread):
def __init__(self, parent=None):
QThread.__init__(self, parent)
self.COMBINATIONS = [
{keyboard.KeyCode(char='0')},
{keyboard.KeyCode(char='1')},
{keyboard.KeyCode(char='2')},
{keyboard.KeyCode(char='3')},
]
self.caspar = None
self.current = set()
def run(self):
self.Connect()
self.Listen()
def exit(self, i):
if self.caspar:
self.caspar.close()
sys.exit(i)
def Connect(self):
try:
self.caspar = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.caspar.connect(("127.0.0.1", 10000))
print("Connected to caspar")
except socket.error:
print("CasparCG not running, or incorrect settings.xml")
self.exit(0)
def execute(self, k=None): # k is videofile
if self.caspar:
movie = bytes("PLAY 1-20 {} \r\n".format(k), 'utf8')
self.caspar.send(movie)
def on_press(self, key):
if any([key in COMBO for COMBO in self.COMBINATIONS]):
self.current.add(key)
if any(all(k in self.current for k in COMBO) for COMBO in self.COMBINATIONS):
self.execute(key)
def on_release(self, key):
if any([key in COMBO for COMBO in self.COMBINATIONS]):
self.current.remove(key)
def Listen(self):
with keyboard.Listener(on_press=self.on_press, on_release=self.on_release) as listener:
listener.join()