gpt4 book ai didi

Python、Tkinter、Subprocess-获取标准输出并将其插入文本

转载 作者:行者123 更新时间:2023-12-02 10:17:51 24 4
gpt4 key购买 nike

请怜悯,我是初学者。

我正在尝试编写一个非常基本的应用程序来运行“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,它开始工作,并且然后不久之后就变得无响应。

我认为问题与缓冲有关?

最佳答案

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

关于Python、Tkinter、Subprocess-获取标准输出并将其插入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26288050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com