微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

QProcess 无法从 Telethon 的异步函数中读取

如何解决QProcess 无法从 Telethon 的异步函数中读取

我正在 PyQt5 GUI 中从 QProcess 运行 python 文件。 我正在使用 readAllStandardOutput(我也尝试过 readAll)显示从终端到文本框的所有内容 在 python 文件中,我正在使用 Telethon(用于使用电报 API 的库)python 文件一个异步事件处理程序,我正在其中打印一些东西。 但在开始之前,客户端 readAllStandardOutput 读取正常。但是在那之后 print("Sent Hi!") 既没有显示在终端上,也没有 Qprocess 正在读取它。 具有 QProcess 声明的 Pyqt5 GUI 文件

class MainWindow(QMainWindow):
def __init__(self,parent = None):
    super(QMainWindow,self).__init__(parent)
    self.process = QProcess(self)
    self.process.readyReadStandardOutput.connect(self.stdoutReady)
    self.process.readyReadStandardError.connect(self.stderrReady)
    self.process.started.connect(lambda: print('Started!'))
    self.process.finished.connect(lambda: print('Finished!'))
    self.process.setProcessChannelMode(QProcess.MergedChannels)

    def startMain(self):
        self.process.start('python3',['mainApp2.py'])

    def stdoutReady(self):
        text = str(self.process.readAllStandardOutput())
        self.mainTextBox.setText(str(text.strip()))

python 文件 QProess 将启动

import asyncio
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
print("Main App started")
api_id = #api_id integer
api_hash = "api_hash string"
client = TelegramClient('session',api_id,api_hash,loop = loop )
async def say_after(delay,what):
    await asyncio.sleep(delay)
    print(what)
@client.on(events.NewMessage)
async def my_event_handler(event):
    if 'hello' in event.raw_text:
        await event.reply('Hi!')
        await say_after(1,'Sent Hi!')
        
client.start()
client.run_until_disconnected()

“Main app Started”显示在文本框中,但不显示“Sent Hi!”。 event.reply('Hi!') 工作正常。 我不确定可能是因为它正在创建一个子进程

我想将终端的所有输出重定向到 GUI 中的文本框(如果您有其他方法建议)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。