• python tkinter 使用(三)


    python tkinter 使用(三)

    本篇文章主要讲下tkinter下的filedialog的使用.

    1: askopenfilename

    首先使用tkinter中fiedialog来实现一个简单的文件选择器.

    这里使用askopenfilename()来启动文件选择器,选择成功后打印下所选文件的名称.

    #!/usr/bin/python3
    # -*- coding: UTF-8 -*-
    """
     @Author: zh
     @Time 2023/11/22 下午12:31  .
     @Describe:
    """
    import tkinter as tk
    import tkinter.filedialog
    
    # 创建窗口
    root = tk.Tk()
    root.title("root")
    root.geometry("500x500")
    
    #筛选 /home/zh/下载 目录下的jpg文件.
    def imgSelect(event):
        root.filename = tkinter.filedialog.askopenfilename(initialdir="/home/zh/下载", title="图片选择",
                                               filetypes=(("jpeg files", "*.jpg"), ("all files", "*.*")))
        print(root.filename)     
    
    #筛选所有的mp4文件
    def videoSelect(event):
        root.filename = tkinter.filedialog.askopenfilename(initialdir="/", title="图片选择",
                                               filetypes=(("mp4 files", "*.mp4"), ("all files", "*.*")))
        print(root.filename)
    
    img = tk.Button(text="图片选择")
    img.pack()
    img.bind('<1>',imgSelect)
    
    
    video = tk.Button(text="视频选择")
    video.pack()
    video.bind('<1>',videoSelect)
    
    root.mainloop()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    其中initialdir参数,可以指定目录来选择, filetypes则可以筛选指定的类型的文件.

    2: askopenfile

    askopenfile是用于打开文件对话框的函数,它可以让用户选择一个文件并返回该文件的文件对象.

    代码如下:

    #!/usr/bin/python3
    # -*- coding: UTF-8 -*-
    """
     @Author: zh
     @Time 2023/11/22 下午12:35  .
     @Describe:
    """
    import tkinter as tk
    import tkinter.filedialog
    
    # 创建窗口
    root = tk.Tk()
    root.title("root")
    root.geometry("500x500")
    
    def imgOpen(event):
        file = tkinter.filedialog.askopenfile(initialdir="/home/zh/下载", title="图片选择",
                                               filetypes=(("jpeg files", "*.jpg"), ("all files", "*.*")))
        print(file)
    
    img = tk.Button(text="图片打开")
    img.pack()
    img.bind('<1>', imgOpen)
    root.mainloop()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    initialdir参数指定了对话框打开时的默认目录,title参数指定了对话框的标题,filetypes参数指定了对话框中显示的文件类型

    执行后我们可以看到如下输出:

    <_io.TextIOWrapper name='/home/zh/下载/IMG-20230830-WA0008.jpg' mode='r' encoding='UTF-8'>
    
    • 1

    3: askopenfiles

    askopenfiles与askopenfile类似,不同的地方再于支持多选,输出也是以list形式输出:

    def imgsOpen(event):
        files = tkinter.filedialog.askopenfiles(initialdir="/home/zhouff/下载", title="图片选择",
                                               filetypes=(("jpeg files", "*.jpg"), ("all files", "*.*")))
        print(files)
    img = tk.Button(text="多选图片打开")
    img.pack()
    img.bind('<1>', imgsOpen)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    输出如下:

    [<_io.TextIOWrapper name='/home/zh/下载/IMG-20230830-WA0008.jpg' mode='r' encoding='UTF-8'>, <_io.TextIOWrapper name='/home/zh/下载/IMG-20230830-WA00081111.jpg' mode='r' encoding='UTF-8'>]
    
    • 1

    4: askdirectory

    askdirectory函数用于弹出一个对话框后让用户选择一个目录,并返回所选目录的路径.

    def askdirectory(event):
        path = tkinter.filedialog.askdirectory()
        print(path)
    img = tk.Button(text="获取路径")
    img.pack()
    img.bind('<1>', askdirectory)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    STM32作业实现(八)触摸按键TPAD
    Vue 商场首页头部布局
    Kubernetes - Kubernetes详解;安装部署(一)
    (28)Blender源码分析之顶层菜单的安装应用模板菜单
    AWS Cloudformation入门项目实践
    新手看过来,带你一次性了解“软考”
    面试提问:为什么不建议在MySQL中使用 utf8?
    JDK8升级到JDK11后Cannot resolve symbol ‘BASE64Encoder‘
    [编程思想录]无锁之CAS
    yamot:一款功能强大的基于Web的服务器安全监控工具
  • 原文地址:https://blog.csdn.net/qq_23025319/article/details/134557656