Python GUI导入其他python文件并运行它

时间:2018-06-11 18:48:26

标签: tkinter python-3.6 file-import

我尝试过导入&在没有GUI的情况下运行Formatting.py脚本,它运行正常。但是当我在GUI中执行此操作时,一旦我点击Run按钮,窗口就进入(Not Responding)模式,甚至没有执行一行确认功能。 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 data\File_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()

如果名称 =='主要':     Cbuttons()

thread = threading.Thread(target=import_path(r'Z:\Structured data\File_Formatting.py'), args=())
        thread.daemon = True  # Daemonize thread
        thread.start()

1 个答案:

答案 0 :(得分:0)

如果需要10分钟才能运行并且您在UI线程上执行它,那么很有可能导致您遇到问题。 See if you can run it in a background thread.