我在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)
我也遇到过同样的问题。我的解决方案就是让我的任务在一个单独的线程上运行,我将其标记为deamon。这样做的结果是,当我销毁所有其他线程(read window.destroy)时,只剩下这个任务线程,由于它被设置为一个deamon线程,程序被成功终止
编辑:
如果不想完全结束Python程序,那么应该查看this answer
相关问题 更多 >
编程相关推荐