我尝试在没有GUI的情况下导入和运行 Formatting.py 脚本,它工作得很好。但是当我在 GUI 中执行此操作时,一旦我点击"运行"按钮,窗口就会进入(无响应(模式,甚至没有执行一行确认功能。 Formatting.py 需要 10 分钟才能运行,但我认为执行不会因此而停止。
如果我从 GUI 代码中删除导入路径行。这段代码也可以正常工作。
我在这里可能错过了什么?
class Cbuttons:
def __init__(self):
self.root = tk.Tk()
self.root.title("Checklist")
self.CheckVar1 = tk.IntVar()
self.CheckVar2 = tk.IntVar()
self.CheckVar3 = tk.IntVar()
self.CheckVar4 = tk.IntVar()
self.label1_text = StringVar()
self.label2_text = StringVar()
self.label3_text = StringVar()
self.label4_text = StringVar()
self.B1 = Button()
self.B2 = Button()
self.L5 = Label()
self.main()
def confirm(self):
self.B1['state'] = 'disabled'
self.B2['state'] = 'active'
if self.CheckVar1.get():
self.label1_text.set("Running File Formatting report. Start Time: "+ str(datetime.now()))
# file_formatting_qad = import_path(r'Z:Structured dataFile_Formatting.py')
if self.CheckVar2.get():
self.label2_text.set("Running report 1")
if self.CheckVar3.get():
self.label3_text.set("Running report 2")
if self.CheckVar4.get():
self.label4_text.set("Running report 3")
self.L5['text']='Reports Processed. It is now safe to close this Window!'
def main(self):
C1 = tk.Checkbutton(self.root, text="File Formatting", variable=self.CheckVar1, anchor='w', onvalue=1,
offvalue=0, height=1, width=40)
L1 = Label(self.root, textvariable=self.label1_text, state='disabled')
C2 = tk.Checkbutton(self.root, text="Physicians Training Report", variable=self.CheckVar2, anchor='w', onvalue=1,
offvalue=0, height=1, width=40)
L2 = Label(self.root, textvariable=self.label2_text, state='disabled')
C3 = tk.Checkbutton(self.root, text="Initiated Cmplaints & Inquiries", variable=self.CheckVar3, anchor='w', onvalue=1,
offvalue=0, height=1, width=40)
L3 = Label(self.root, textvariable=self.label3_text, state='disabled')
C4 = tk.Checkbutton(self.root, text="Design Related Complaints", variable=self.CheckVar4, anchor='w', onvalue=1,
offvalue=0, height=1, width=40)
L4 = Label(self.root, textvariable=self.label4_text,state='disabled')
self.B1 = Button(self.root, text="RUN", command=self.confirm)
self.B2 = Button(self.root, text="STOP", command=self.root.destroy,state = 'disabled')
self.L5 = Label(self.root, text="Do not Hit 'STOP' Button or Close this Window untill this Text changes to Reports processed")
C1.pack()
C2.pack()
C3.pack()
C4.pack()
L1.pack()
L2.pack()
L3.pack()
L4.pack()
self.B1.pack()
self.B2.pack()
self.L5.pack()
self.root.mainloop()
如果名称== 'main': 纽扣((
thread = threading.Thread(target=import_path(r'Z:Structured dataFile_Formatting.py'), args=())
thread.daemon = True # Daemonize thread
thread.start()
如果运行需要 10 分钟,并且您在 UI 线程上执行它,那么很有可能这就是您遇到问题的原因。查看是否可以在后台线程中运行它。