I have a script which opens a file selection window using the tinter module. I've run it without issue on a windows computer, but on a Mac (Ventura 13.3.1) the window doesn't close after selecting a file. It persists as a small, blank window titled tk and as an application
in my Dock titled python3.10. I also see the following notice when I run the script:2023-06-30 11:17:10.566 python[42030:4586512] +[CATransaction synchronize] called within transaction
The Python code:
from tkinter import *
from tkinter import filedialog
def get_file():
root = Tk()
root.wm_attributes('-topmost', 1)
root.withdraw()
selected_file = filedialog.askopenfilename(initialdir='',
title="File Selection",
parent=root)
return selected_file
print('Select .csv File with Relevant Absorption Spectra')
data_path = get_file()
You create a root window when you instantiate tk with
root = Tk(), but because of the way Mac OS handles quitting apps, closing the window isn't enough to exit. You need to callroot.quit().Also, setting
'-topmost'here is unnecessary since you're immediately minimizing the window.Lastly, star imports are usually regarded as a bad idea. To avoid namespace pollution you'd typically use
import tkinter as tkand then prefix your tkinter objects withtk.like I've done withroot = tk.Tk()All that said, you'll obviously want to do whatever you need to to with
data_pathbefore the call toroot.quit()Hope that helps, and happy coding!