我有一个工作代码,可以按我的意愿打印输出,但是我的应用程序处于冻结状态,直到不再响应为止。我正在使用tkinter,python3.7。我已经阅读了很多有关线程和排队的知识,但是由于我是Python的新手,所以我不知道如何启动。
这是我的主要代码:
from tkinter import *
class App(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.grid(row=0, column=0, sticky=W+E+N+S)
self.build_widgets()
def run_script1(self): #COUNT
sys.stdout = self
try:
del (sys.modules["COUNT"])
except:
pass
import COUNT
COUNT
sys.stdout = sys.__stdout__
def write(self, txt):
self.useroutput.insert(INSERT, txt)
self.update_idletasks()
def build_widgets(self):
self.label1 = Label(self, text="TEST", font="bold")
self.label1.grid(row=0, column=0, sticky=W+E+N+S, padx=5, pady=5)
self.button2 = Button(self, text="COUNT", command=self.run_script1)
self.button2.grid(row=1, column=0, sticky=W+E+N+S, padx=5, pady=5)
self.label1 = Label(self, text="Logs:")
self.label1.grid(row=2, column=0, sticky=W+E+N+S, padx=5, pady=5)
self.useroutput = Text(self, width=70, height=20)
self.useroutput.grid(row=3, column=0, sticky=W+E+N+S, padx=1, pady=1)
self.quitbutton = Button(self, text="Quit", command=quit)
self.quitbutton.grid(row=4, column=0, sticky=W+E+N+S, padx=5, pady=5)
self.scroll = Scrollbar(self, orient="vertical", command=self.useroutput.yview)
self.scroll.grid(row=3, column=1, sticky=N+S, padx=5, pady=5)
root = Tk()
root.resizable(False, False)
root.title("Ping Tool")
app = App(master=root)
app.mainloop()
这是它正在调用的子代码,称为“ COUNT”,它位于同一目录中:
import time
for i in range(1,1000):
print(i)
time.sleep(2)