0

I created a simple GUI in python 3.4 using tkinter 8.5. I used cx_freeze to build an exe from this GUI. Now when I run this exe, sometimes I notice that the program still shows under 'Background Processes' in Task Manager even after I terminate it using a Quit button or using the close button in the window.

The GUI works like this: You select a file type from a drop down list, read the file using a command button and save it as a separate file. Now this problem happens only if I close the GUI after using it. If I simply open the GUI and close it using the Quit button or close button, it does not stay as a background process.

Is it normal for it to behave like this? If not what can I do to terminate it properly?

The simplified code for the GUI is given below. The function 'fileselect' calls functions from the module 'dataselect'. If needed, I will provide the code for the 'dataselect' module also.

from dataselect import *
from openpyxl import Workbook
from tkinter import *
from tkinter import ttk, filedialog

root = Tk()
root.title("Select Data File")

# Actual File Selection based on Combobox Selection
def fileselect():
    file_type = filetype.get()
    if file_type == ".txt":
        text = selecttxt()
        textfile = filedialog.asksaveasfile(mode='w', defaultextension=".txt")
        for line in text:
            for number in line:
                textfile.write(str(number)+" ")
            textfile.write('\n')
    elif file_type == ".xlsx":
        excel = selectxlsx()
        excelfile = filedialog.asksaveasfile(mode='w', defaultextension=".xlsx")
        excelfilename = excelfile.name
        excelbook = Workbook()
        excelsheet = excelbook.active
        rows = 0
        for excel_row in excel:
            cols = 0
            for excel_cell in excel_row:
                excelsheet.cell(row=rows, column=cols).value = excel[rows][cols]
                cols += 1
            rows += 1
        excelbook.save(excelfilename)

def quit():
    global root
    root.destroy()

# Select the File Type to be opened (.txt or .xlsx for now)
ttk.Label(root, text="Please select the file type").grid(column=2, row=1)
filetype = StringVar()
sel_type = ttk.Combobox(root,values=('.txt','.xlsx'),textvariable=filetype)
sel_type.grid(column=2,row=2,sticky=E)

# Command Button for Opening File
cb_open = ttk.Button(root, text="Select File", command=fileselect)
cb_open.grid(column=2, row=3)

# Command Button for Quitting GUI
cb_quit = ttk.Button(root, text="Quit", command=quit)
cb_quit.grid(column=1, row=3)

root.mainloop()
Anish
  • 3
  • 4
  • possible duplicate of [Function to close the window in Tkinter](http://stackoverflow.com/questions/8009176/function-to-close-the-window-in-tkinter) – Sait Jul 01 '15 at 21:35
  • @Sait - The answer suggested in that post does not solve my problem since I already have the parentheses in root.destroy() – Anish Jul 01 '15 at 21:48
  • Do you have `root.protocol("WM_DELETE_WINDOW", root.destroy)` in your code? Have you tried to create a dummy example with a single `'Quit'` button and see whether the issue is still present? Or you could start removing code from your application until the issue disappears. Unrelated: `global root` is unnecessary here. – jfs Jul 01 '15 at 22:45
  • @J.F.Sebastian - I modified my question after studying the behavior. I do not have the line you mentioned in my code, but this issue occurs for both the Quit button that I created and the close button on the window. – Anish Jul 02 '15 at 19:41
  • does `openpyxl` create a background process /thread that you should shutdown in `quit()`? – jfs Jul 03 '15 at 12:16

1 Answers1

1

There are two things you need to change: 1) Add sys.exit() to your quit method

    def quit():
        root.quit
        root.destroy
        sys.exit()

2) Add protocol to your root

    root.protocol("WM_DELETE_WINDOW", quit)

Finally, don't forget to import sys.

Ernest
  • 31
  • 3