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()

其中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()

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

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

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

3: askopenfiles

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

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

输出如下:

[<_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'>]

4: askdirectory

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

def askdirectory(event):
path = tkinter.filedialog.askdirectory()
print(path)
img = tk.Button(text="获取路径")
img.pack()
img.bind('<1>', askdirectory)

python tkinter 使用(三)的更多相关文章

  1. Python Tkinter Entry(文本框)

    Python学习记录--关于Tkinter Entry(文本框)的选项.方法说明,以及一些示例. 属性(Options) background(bg) borderwidth(bd) cursor e ...

  2. Python Tkinter学习笔记

    介绍 入门实例 显示一个窗口,窗口里面有一个标签,显示文字 import tkinter as tk # 一个顶层窗口的实例(Top Level),也称为根窗口 app = tk.Tk() # 设置窗 ...

  3. 进击的Python【第三章】:Python基础(三)

    Python基础(三) 本章内容 集合的概念与操作 文件的操作 函数的特点与用法 参数与局部变量 return返回值的概念 递归的基本含义 函数式编程介绍 高阶函数的概念 一.集合的概念与操作 集合( ...

  4. Python 基础语法(三)

    Python 基础语法(三) --------------------------------------------接 Python 基础语法(二)------------------------- ...

  5. 笨办法学 Python (第三版)(转载)

    笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html   摘自https://learn-python ...

  6. Python/MySQL(三、pymysql使用)

    Python/MySQL(三.pymysql使用) 所谓pymysql就是通过pycharm导入pymysql模块进行远程连接mysql服务端进行数据管理操作. 一.在pycharm中导入pymysq ...

  7. python学习第三次记录

    python学习第三次记录 python中常用的数据类型: 整数(int) ,字符串(str),布尔值(bool),列表(list),元组(tuple),字典(dict),集合(set). int.数 ...

  8. python中的三种输入方式

    python中的三种输入方式 python2.X python2.x中以下三个函数都支持: raw_input() input() sys.stdin.readline() raw_input( )将 ...

  9. python 历险记(三)— python 的常用文件操作

    目录 前言 文件 什么是文件? 如何在 python 中打开文件? python 文件对象有哪些属性? 如何读文件? read() readline() 如何写文件? 如何操作文件和目录? 强大的 o ...

  10. 3.Python爬虫入门三之Urllib和Urllib2库的基本使用

    1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器解释才呈现出来的,实质它是一段HTML代码,加 JS.CSS ...

随机推荐

  1. Java实践项目 - 商品分类

    Smiling & Weeping ---- 好想回到那个拉钩许诺的年代 1.1商品分类的思路:一次性查询三级分类 (一级为美味麒麟榜,二级为闭眼入,第三级为商品) 优点:只需要一次查询,根据 ...

  2. (数据科学学习手札154)geopandas 0.14版本新特性一览

    本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 大家好我是费老师,就在前两天,Python生态中 ...

  3. Solution -「THUPC 2021」区间矩阵乘法

    Description Link. 给定长度为 \(n\) 的序列 \(a_1, a_2, \dots, a_n\):共 \(m\) 组询问,每次询问给出 \(d,p_1,p_2\),求 \[\sum ...

  4. flask出现This is a development server. Do not use it in a production deployment. Falsk WSGI两种解决办法

    WARNING: This is a development server. Do not use it in a production deployment. Falsk WSGI "这个 ...

  5. 再谈http请求调用(Post与Get),项目研发的核心一环

    支持.Net Core(2.0及以上)与.Net Framework(4.0及以上) [目录] 前言 Post请求 Get请求 与其它工具的比较 1[前言] http请求调用是开发中经常会用到的功能. ...

  6. [GXYCTF 2019]BabyUpload

    看到题目是一个文件上传 就先随便传的东西试试,看有什么过滤之类的 上传一个一句话木马,提示后缀名不能为ph 随便上传了带有一句话木马的图片,发现上传成功,但这个图片不能直接利用,要先上传一个.htac ...

  7. NW排错

    fist date VM备份失败时: NW server上(linux): > nsradmin >p type : nsr recover > cd /nsr/logs >n ...

  8. Sum of MSLCM 题解

    Sum of MSLCM 题目大意 定义 \(\text{MSLCM}(n)\) 为所有满足该数集的 \(\text{lcm}\) 为 \(n\) 的数集中元素个数最多的数集的所有数字的和,现有多次询 ...

  9. node(1)

    1.新建http.js //node搭建http服务器 let http=require('http'); //使用http建立服务请求 http.createServer(function(requ ...

  10. P1844 阅览室

    此题现有题解较为冗长,因此前来贡献一发最短解. 首先正常的思路是直接按题意模拟.即: 枚举当前时刻 \(T\) 对于每个人,标记该时刻想要拿到的书 根据题目的要求判断冲突情况 对书进行分配 实现起来复 ...