<blockquote>
<p>Trying to get the data to stream into the text box, but after all I've
tried, I only seem to be able to get it to give all the output at
once.</p>
</blockquote>
<p>此代码如下:</p>
<pre><code>def selection():
fileList = listbox.curselection()
for file in fileList:
os.chdir(fldrPath)
# Right here is the problematic section
with Popen(["powershell.exe", '-File', fldrPath + '\\' + listbox.get(file)], stdout=PIPE, bufsize=1,
universal_newlines=True) as p:
for line in p.stdout:
output.insert('end', line)
print(line, end='')
</code></pre>
<p>完成后,您可以看到它执行了什么。要更新它,您可以使用tkinter的<code>after</code>方法并检查给定时间内的更改。
就像这个例子一样</p>
<p>tkinter主循环的工作方式类似于<a href="https://wiki.tcl-lang.org/page/Tcl+event+loop" rel="nofollow noreferrer">this</a></p>
<pre><code> Start
|
|< +
v ^
Do I have No[*] Calculate how Sleep for at |
work to do? -> long I may sleep -> most that much ->|
| time |
| Yes |
| |
v |
Do one callback |
| |
+ -+
</code></pre>
<p>另一项建议是使用另一个<a href="https://stackoverflow.com/a/63416839/13629335">thread</a>。
你也应该阅读<code>.communicate()</code>的答案<a href="https://stackoverflow.com/a/16770371/13629335">here</a></p>