[Python] python - how to center tkinter fileDialog window

Discussão em 'Python' iniciado por Stack, Março 28, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página