• 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
  • 相关阅读:
    常见的部署类型(停机部署、蓝绿部署、滚动部署、灰度部署、AB测试等)
    C专家编程 第3章 分析C语言的声明 3.1 只有编译器才会喜欢的语法
    【DNS系列】什么是正向DNS和反向DNS
    python常见面试考点
    第八章:关系数据库设计
    c++新经典—c++基本语言
    数据链路层、网络层以及IP协议
    Java-Enum常量特定方法
    【附源码】Python计算机毕业设计软件工程在线学习平台
    佳能e478打印机怎么连接wifi
  • 原文地址:https://blog.csdn.net/qq_23025319/article/details/134557656