admin 管理员组文章数量: 1086019
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.
本文标签: pythonhow to center tkinter fileDialog windowStack Overflow
版权声明:本文标题:python - how to center tkinter fileDialog window - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744046112a2524147.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论