I would like to open an audio file in python using a tkinter fileDialog. How can I center the fileDialog window?
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
root.attributes('-topmost', True)
audio1 = "*.mp3 *.wav"
videos1 = "*.mp4 *.mkv"
file_path = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select file",filetypes=[("audio files", audio1), ("video files", videos1)])
file_name = os.path.basename(file_path)
I would also like to keep the root
hidden ... with the current code root.withdraw()
And one more thing, is it possible to have the fileDialog window fall to the background (behind another window), when I select another window to be the active window? I'm currently using, root.attributes('-topmost', True)
... to force my fileDialog
to open ontop of all other windows, but if I want to browse some other windows while it is still open ... then I cannot.
root = tk.Tk()
root.geometry("+{}+{}".format(int(root.winfo_screenwidth() / 2 - root.winfo_reqwidth() / 2) - 400, int(root.winfo_screenheight() / 2 - root.winfo_reqheight() / 2) - 300))
root.withdraw()
input1 = filedialog.askopenfilename(parent=root, initialdir=os.getcwd(), title='Select directory')
root.destroy()
I have solved the problem with a this fix. Its not an official solution, but there is no way to `call` the true dimensions of the filedialog window, in order to center it 100% ... So I have eyed the solution by hand.