如何在关闭tkinter框架时杀死当前正在运行的函数?

2024-09-29 01:19:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我在tkinter frame中有一个导出按钮,一旦我点击它,它就会将一些数据导出到CSV文件。我在同一个框架中还有一个logout按钮。我在logout按钮中添加了一个函数,用于获取mainframe并终止当前帧

现在,当我单击导出按钮时,需要一分钟才能完成。在那之前,如果我点击logout按钮,它会把我带到mainframe。但出口进程正在落后,并没有被扼杀。我仍然得到一个csv文件

一旦我点击了logout按钮,我想同时终止正在运行的exportfunction。我该怎么做

我使用了self.destroy(),但是导出函数没有被终止。 下面几行是我代码的一部分,主代码有400多行PageOne是我拥有导出注销按钮的框架

class PageOne(ttk.Frame):
    def __init__(self, master):
        ttk.Frame.__init__(self, master, height=800, width=1400)

        self.logOut_btn = ttk.Button(self, text="Logout", command=self.logOut)
        self.logOut_btn.place(x=1280, y=15)
        self.export = ttk.Button(self, text="Export", command=self.export_cubeData)
        self.export.place(x=620, y=y1+50)
        self.exportPath = ttk.Label(self, text="Export Path", font='Helvetica 12 bold')
        self.exportPath.place(x=430, y=y1+100)
        self.entry_exportPath = ttk.Entry(self)
        self.entry_exportPath.place(x=540, y=y1+100, width=450)
        final_df = pd.Dataframe()


    def logOut(self):
        self.destroy()
        self.master.switch_frame(StartPage)
        with TM1Service(**config['TM1']) as tm1:
            tm1.logout()


    def setFlag(self):
        self.flag = 1

    def export_cubeData(self):
        exportPath = self.entry_exportPath.get()
        for i in range(10):
            self.update()
            final_df.to_csv(exportPath)

Tags: 文件textselfmasterdefplaceexport按钮
1条回答
网友
1楼 · 发布于 2024-09-29 01:19:38

我也遇到过同样的问题。我的解决方案就是让我的任务在一个单独的线程上运行,我将其标记为deamon。这样做的结果是,当我销毁所有其他线程(read window.destroy)时,只剩下这个任务线程,由于它被设置为一个deamon线程,程序被成功终止

编辑:

如果不想完全结束Python程序,那么应该查看this answer

相关问题 更多 >