python - macOS tkinter : how does filetypes of askopenfilename work

标签 python python-3.x macos tkinter file-type

我的问题

  • 无法切换 filetypesFilter (见下图)因为它们处于灰色模式,如果设置 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文件将可以在窗口中选择。

    enter image description here

    这是代码:
    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")]
    
    
    

    ![enter image description here
  • 只留下 ("All Files", "*.*") .这就是我所期望的*.*终于生效了。

  • filetypes = [("All Files", "*.*")]
    

    enter image description here

    系统信息
  • macOS Catalina
  • python 3.7.5
  • TkVersion 8.6
  • 最佳答案

    在我的系统上:

  • macOS Mojave 10.4.6
  • Python 3.8.0
  • TkVersion 8.6

  • 我发现这个简单的解决方法让我得到了想要的行为:
    filetypes = [ 
                ("All Files", "*.*"),
                ("Python File", "*.py"), 
                ("Image File", "*.bmp"),
                ]
    
    也就是说,通过让它想出 "All Files"最初,然后在打开的对话框中切换到其他类型适本地过滤文件。

    关于python - macOS tkinter : how does filetypes of askopenfilename work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58799432/

    相关文章:

    iphone - 你如何显示 Xcode 构建日志? (尝试验证 iPhone 发行版构建 zip 是否已正确创建。)

    python - 如何转义 xpath 中的正斜杠?

    Python 从抛出异常的地方继续

    python - 有没有一种简单的方法可以计算 Pandas value_counts 系列的均值和标准差?

    python - 如何在python中将一个csv拆分为多个文件

    python - 类型错误 : metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

    python - wkhtmltopdf 在每次运行时生成不同的校验和

    python - 如何只提取 .tar.gz 成员的文件?

    xcode - 为 Mac OS 命令行工具编写 XCTestCase

    r - 在 R、OSX 10.11.6 中绘制图表时,ÅÄÖ(瑞典字符)不可见