macOS tkinter:askopenfilename 的文件类型如何工作 [英] macOS tkinter: how does filetypes of askopenfilename work
本文介绍了macOS tkinter:askopenfilename 的文件类型如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 无法在
Filter
(见下图)中的filetypes
之间切换,因为它们处于灰色模式,如果将filetypes
设置如下
- Can't switch amongst
filetypes
in theFilter
(see picture below) since they are in grey mode, if setfiletypes
as below
filetypes = [
("Python File", "*.py"),
("Image File", "*.bmp"),
("All Files", "*.*")
]
- 虽然默认文件类型是
.py
,我们也可以在窗口中选择.bmp
,因为test.bmp
被突出显示.这意味着可以同时激活filetypes
、.py
和.bmp
.这种Filter
行为正常吗?
- Though the default filetype is
.py
we can also choose.bmp
in the window since thetest.bmp
is highlighted. This means thefiletypes
,.py
and.bmp
, can be activated at the same time. Is thisFilter
behavior normal?
我期望的是我们可以从 filetypes
集中挑选出一种类型,并且这些选项应该是 互斥
,即,如果选择 Python File(.py)
在 Filter
中,然后只有 .py
文件可以在窗口中选择.
What I expect is that we can single out one type from the set of filetypes
and these options should be mutually exclusive
, i.e., if select Python File (.py)
in the Filter
, then ONLY .py
file will be available to choose in the window.
from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askopenfilename
# from tkinter.filedialog import askopenfile
# from tkinter.filedialog import askopenfilenames
filetypes = [
("Python File", "*.py"),
("Image File", "*.bmp"),
("All Files", "*.*")
]
def OpenFile():
p = askopenfilename(initialdir="../",
filetypes =filetypes,
title = "Choose a file.")
print ("Path to File: \n", p)
#Using try in case user types in unknown file
# or closes without choosing a file.
# try:
# with open(p, 'r') as f:
# print("Content of File:\n", f.read())
# except:
# print("Error!")
root = Tk()
root.title( "File Opener")
label = ttk.Label(root,
text ="File Read Test!",
foreground="red",
font=("Helvetica", 16))
label.pack()
menu = Menu(root)
root.geometry("300x200")
root.config(menu=menu)
file = Menu(menu)
file.add_command(label = 'Open', command = OpenFile)
file.add_command(label = 'Exit', command = root.quit)
menu.add_cascade(label = 'File', menu = file)
root.mainloop()
更多示例
- 如果删除
("All Files", "*.*")
会怎样?仍然无法在文件类型之间切换,并且.py
和.bmp
都处于活动状态.并且所有其他文件类型都超出了先前设置的范围. - What if delete
("All Files", "*.*")
? Still can not switch between file types and both.py
and.bmp
are active. And all other file types are out of scope the same as prveious settings.
more examples
filetypes = [
("Python File", "*.py"),
("Image File", "*.bmp")]
- 只留下
("All Files", "*.*")
.这正是我所期望的,*.*
终于生效了.
- Leave only
("All Files", "*.*")
. This is what I expect and*.*
finally comes to effect.
filetypes = [("All Files", "*.*")]
- macOS Catalina
- python 3.7.5
- Tk 版本 8.6
推荐答案
在我的系统上:
- macOS Mojave 10.4.6
- Python 3.8.0
- Tk 版本 8.6
我发现这个简单的解决方法让我得到了想要的行为:
I found this simple workaround got me the desired behavior:
filetypes = [
("All Files", "*.*"),
("Python File", "*.py"),
("Image File", "*.bmp"),
]
也就是说,通过它最初提供所有文件"
,然后在打开的对话框中切换到其他类型来适当地过滤文件.
That is, by having it come up with "All Files"
initially, then switching to the other types in the open dialog filtered the files appropriately.
这篇关于macOS tkinter:askopenfilename 的文件类型如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文