如何将 Python 控制台输出重定向到 QTextBox

发布于 2024-09-02 07:44:01 字数 233 浏览 5 评论 0原文

我正在开发一个用于重新编译 Linux 内核的 GUI。为此,我需要从 Python 实现 4-5 个 Linux 命令。我使用 Qt 作为 GUI 设计器。我已使用 os.system() 调用成功实现了命令。但输出是在控制台获得的。真正的问题是命令的输出是一个列表,需要几乎 20-25 分钟的连续打印。我们如何将此控制台输出传输到 Qt 中设计的文本框。任何人都可以帮助我在 Qt 中使用源作为实时控制台输出来实现 setSource() 操作。

I'm working on developing a GUI for the recompilation of Linux kernel. For this I need to implement 4-5 Linux commands from Python. I use Qt as GUI designer. I have successfully implemented the commands using os.system() call. But the output is obtained at the console. The real problem is the output of command is a listing that takes almost 20-25 min continuous printing. How we can transfer this console output to a text box designed in Qt. Can any one help me to implement the setSource() operation in Qt using source as the live console outputs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稍尽春風 2024-09-09 07:44:01
self.process = QProcess()
self.connect(self.process, SIGNAL("readyReadStdout()"), self.readOutput)
self.connect(self.process, SIGNAL("readyReadStderr()"), self.readErrors)
tarsourcepath="sudo tar xvpf "+ self.path1
self.process.setArguments(QStringList.split(" ",tarsourcepath))
self.process.start()



def readOutput(self):

    self.textBrowser2.append(QString(self.process.readStdout()))
    if self.process.isRunning()==False:
        self.textBrowser2.append("\n Completed Successfully")




def readErrors(self):
    self.textBrowser2.append("error: " + QString(self.process.readLineStderr()))

这对我来说工作做得很好。谢谢大家。

self.process = QProcess()
self.connect(self.process, SIGNAL("readyReadStdout()"), self.readOutput)
self.connect(self.process, SIGNAL("readyReadStderr()"), self.readErrors)
tarsourcepath="sudo tar xvpf "+ self.path1
self.process.setArguments(QStringList.split(" ",tarsourcepath))
self.process.start()



def readOutput(self):

    self.textBrowser2.append(QString(self.process.readStdout()))
    if self.process.isRunning()==False:
        self.textBrowser2.append("\n Completed Successfully")




def readErrors(self):
    self.textBrowser2.append("error: " + QString(self.process.readLineStderr()))

This did the work quite good for me. thank you all.

夜血缘 2024-09-09 07:44:01

我主要处理 wxPython,但是 http://diotavelli.net/PyQtWiki/Capturing_Output_from_a_Process 是一个解决方案会起作用吗?

从页面:

问题:您想要运行一个进程
打印大量信息到
控制台并将输出显示在
文本编辑器或浏览器,但结果
是一个冻结直到
过程已完成。

解决方案(众多可能的解决方案之一):
创建一个QProcess对象,连接它
向班级中的某些位置发出信号,
向其传递所需的参数并
开始吧。进程标准输出上的数据
并且 stderr 被传送到您的插槽。

继续...

I mostly deal with wxPython, but is http://diotavelli.net/PyQtWiki/Capturing_Output_from_a_Process a solution that would work?

From the page:

Problem: You want to run a process
that prints lots of information to the
console and display the output in a
text editor or browser, but the result
is a GUI that freezes until the
process is finished.

Solution (one of many possible):
Create a QProcess object, connect its
signals to some slots in your class,
pass it the required arguments and
start it. Data on the process's stdout
and stderr is delivered to your slots.

continued...

辞取 2024-09-09 07:44:01

我想到了使用管道。您可以使用后台线程来读取程序的输出(并在添加新行时将事件发送到 GUI)。

所以基本的想法是这样的:

os.chdir("/usr/src/linux-2.6.34")

p = os.popen("make", "r")
try:
    while True:
        line = p.readline()
        if not line:
            break

        # Replace this with a GUI update event (don't know anything about Qt, sorry)
        print line
finally:
    # Cf. http://docs.python.org/library/os.html#os.popen
    programReturnValue = p.close() or 0

Using a pipe comes to mind. You could use a background thread that reads the output of the program (and sends events to the GUI whenever a new line is added).

So the basic idea is this:

os.chdir("/usr/src/linux-2.6.34")

p = os.popen("make", "r")
try:
    while True:
        line = p.readline()
        if not line:
            break

        # Replace this with a GUI update event (don't know anything about Qt, sorry)
        print line
finally:
    # Cf. http://docs.python.org/library/os.html#os.popen
    programReturnValue = p.close() or 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文