1

I have an export button in tkinter frame, once I click that it exports some data to CSV file. I also have a logout button in the same frame. I have added a function it the logout button to take me main frame and kill the current frame.

Now when I click the export button it will take a minute to complete. Before that if I click the logout button, it takes me to main frame. But the export process is running behind and does not get killed. I'm still getting a csv file.

Once I click logout button, I would like to kill the running export function as well. How do I do that?

I used self.destroy() but the export function is not getting killed. Below lines are part of my code, the main code has over 400 lines. PageOne is the frame in which I have the export and Logout buttons.

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)
martineau
  • 119,623
  • 25
  • 170
  • 301
user1404
  • 179
  • 1
  • 3
  • 10
  • You're going to need to add some details, ideally a [mcve]. Normally, a function in a Tkinter program that continues to run will freeze the GUI, so the situation you describe is impossible. Perhaps you are running the function in a separate thread, or a separate process? – jasonharper Mar 27 '19 at 16:20
  • I have shared a sample code, please check if that helps! I use `self.update()` in a loop so the GUI doesn't freeze. I'm not using `multi threading`. – user1404 Mar 27 '19 at 16:42
  • 1
    You could set a flag when the button is pressed, and in your export function check this flag every time through the loop, and exit if it's set. – jasonharper Mar 27 '19 at 16:52
  • I already have a `method` for setting the flag, could use the same. How stupid I have been for the past hour. Thanks Jason, you got it right. – user1404 Mar 27 '19 at 17:09

1 Answers1

0

I ran into the same problem once. My solution was simply to have my task run on a separate thread, which I flagged as deamon. What this did, is when I destroyed all other threads (read window.destroy), only this task thread was left, and since it was set as a deamon thread, the program was terminated successfully.

Edit :

You should look at this answer if you don't want to end your Python program completely.

Olivier Samson
  • 609
  • 4
  • 13