最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - how to center tkinter fileDialog window - Stack Overflow

programmeradmin6浏览0评论

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.

Share Improve this question asked Mar 28 at 9:30 RhysRhys 5,28216 gold badges44 silver badges65 bronze badges 3
  • 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
Add a comment  | 

1 Answer 1

Reset to default 0
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.

发布评论

评论列表(0)

  1. 暂无评论