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.
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.
- Filedialog is shown relative to the root. You can set the geometry of the root window to change its position. – 8349697 Commented Mar 30 at 18:47
- that probably depends on the operating system. someone tried this on windows and the dialog did not show up in the center. – Christoph Rackwitz Commented Mar 31 at 0:33
- Create a 'temporary' toplevel window then centered then a file dialog opened it will also be centered - definitely messy, pity that any/all dialogs don't have x/y positioning options - haven't tried (yet) – ticktalk Commented Mar 31 at 8:08
1 Answer
Reset to default 0root = 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.