filetypes
设置如下,则它们处于灰色模式,因此无法在Filter
中的filetypes
之间切换(请参见下图)filetypes = [
("Python File", "*.py"),
("Image File", "*.bmp"),
("All Files", "*.*")
]
.py
,但由于.bmp
高亮显示,因此我们也可以在窗口中选择test.bmp
。这意味着可以同时激活filetypes
,.py
和.bmp
。此Filter
行为正常吗? 我期望我们可以从filetypes
的集合中选择一种类型,这些选项应该是mutually exclusive
,即,如果在Python File (.py)
中选择Filter
,则只有.py
文件会可在窗口中选择。
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
均处于活动状态。所有其他文件类型都超出了常用设置的范围。filetypes = [
("Python File", "*.py"),
("Image File", "*.bmp")]
("All Files", "*.*")
。这就是我的期望,*.*
终于生效。filetypes = [("All Files", "*.*")]
我在相同的macOS上遇到了相同的问题。 :(