我有这个代码,我已经为测试做了一个计算脚本但是time.sleep冻结了textfield
import subprocess
import tkinter as tk
from threading import Thread
import os
ospath = os.path.dirname(os.path.abspath(__file__))
path = (ospath +"\\calcule.py")
master = tk.Tk()
master.geometry('930x450+400+400')
def sudo(cmd, terminal):
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, universal_newlines=True, shell = True)
p.poll()
print("2")
while True:
line = p.stdout.readline()
terminal.insert(tk.END, line)
terminal.see(tk.END)
if not line and p.poll is not None: break
while True:
print("3")
err = p.stderr.readline()
terminal.insert(tk.END, err)
terminal.see(tk.END)
if not err and p.poll is not None: break
terminal.insert(tk.END, '\n Finished download')
textfield = tk.Text(master, font = "Arial 10", bg = "#000000", fg = "yellow")
textfield.place(x=5, y=5, width=400, height=250)
t = Thread(target = lambda: sudo(path, textfield))
t.start()
tk.mainloop()
我已经为测试做了另一个脚本
import time
a = 0
b = 1
while a < 1:
print(b)
b += 1
time.sleep(1)
if b == 1000:
a +=1
time.sleep冻结代码,我不知道如何调试此代码。