【问题标题】:Python, Tkinter, Subprocess- getting stdout and inserting it to TextPython,Tkinter,Subprocess-获取标准输出并将其插入文本
【发布时间】:2014-12-04 22:51:03
【问题描述】:

请原谅,我是初学者。

我正在尝试编写一个非常基本的应用程序来运行“chkdsk c:”并将输出逐行打印到文本框中。对于每一行,我想要一个 ttk.Progressbar 移动一点。我在这里看到过类似的问题得到了回答,但无法让它们发挥作用。我已经走到这一步了:

from Tkinter import *
import ttk
from subprocess import Popen, PIPE

gui = Tk()

text = Text(gui)
lb = ttk.Label(text="Chkdsk")
prog = ttk.Progressbar(mode='indeterminate')

def chkdsk():
    proc = Popen(['chkdsk','c:'],stdout=PIPE)
    while True:
        line = proc.stdout.readline()
        if line != '':
            text.insert(INSERT, "\n")
            text.insert(END, line.rstrip())
            prog.step(1)
            gui.update()
        else:
            break

bt = ttk.Button(text="Chkdsk", command=chkdsk).grid(row=4, column=5)

text.grid(row=6, column= 5)
lb.grid(row=3, column=5)
prog.grid(row=7,column=5)
mainloop()

好的,所以当我在 提升的命令提示符下运行这个 .pyw 脚本(这对我的目的很有用) - “python chkdsk.pyw”并启动 chkdsk 时,它开始工作,并且然后很快就没有响应了。

我认为问题与缓冲有关?

【问题讨论】:

  • @J.F.Sebastian 谢谢,我可以轻松地更改一行来完成这项工作。但我无法理解其中的 90%。
  • 如果您对代码有具体问题; ask

标签: python python-2.7 tkinter popen ttk


【解决方案1】:

readline 正在阻塞。使用非阻塞readafter-方法。

【讨论】:

    猜你喜欢
    • 2013-10-30
    • 2013-02-14
    • 2021-01-06
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 2012-01-25
    • 1970-01-01
    相关资源
    最近更新 更多