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()
其中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 使用(三)的更多相关文章
- Python Tkinter Entry(文本框)
Python学习记录--关于Tkinter Entry(文本框)的选项.方法说明,以及一些示例. 属性(Options) background(bg) borderwidth(bd) cursor e ...
- Python Tkinter学习笔记
介绍 入门实例 显示一个窗口,窗口里面有一个标签,显示文字 import tkinter as tk # 一个顶层窗口的实例(Top Level),也称为根窗口 app = tk.Tk() # 设置窗 ...
- 进击的Python【第三章】:Python基础(三)
Python基础(三) 本章内容 集合的概念与操作 文件的操作 函数的特点与用法 参数与局部变量 return返回值的概念 递归的基本含义 函数式编程介绍 高阶函数的概念 一.集合的概念与操作 集合( ...
- Python 基础语法(三)
Python 基础语法(三) --------------------------------------------接 Python 基础语法(二)------------------------- ...
- 笨办法学 Python (第三版)(转载)
笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html 摘自https://learn-python ...
- Python/MySQL(三、pymysql使用)
Python/MySQL(三.pymysql使用) 所谓pymysql就是通过pycharm导入pymysql模块进行远程连接mysql服务端进行数据管理操作. 一.在pycharm中导入pymysq ...
- python学习第三次记录
python学习第三次记录 python中常用的数据类型: 整数(int) ,字符串(str),布尔值(bool),列表(list),元组(tuple),字典(dict),集合(set). int.数 ...
- python中的三种输入方式
python中的三种输入方式 python2.X python2.x中以下三个函数都支持: raw_input() input() sys.stdin.readline() raw_input( )将 ...
- python 历险记(三)— python 的常用文件操作
目录 前言 文件 什么是文件? 如何在 python 中打开文件? python 文件对象有哪些属性? 如何读文件? read() readline() 如何写文件? 如何操作文件和目录? 强大的 o ...
- 3.Python爬虫入门三之Urllib和Urllib2库的基本使用
1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器解释才呈现出来的,实质它是一段HTML代码,加 JS.CSS ...
随机推荐
- 在 Net7.0 环境下使用 RestSharp 发送 Http(FromBody和FromForm)请求
一.简介 最近,在做一个数据传输的服务,我在一个Worker Service里面需要访问 WebAPI 接口,并传输数据,也可以提交数据.由于第一次使用 RestSharp 发送请求,也遇到了很多问题 ...
- Linux部署项目常用命令(持续更新)
防火墙配置 # 启动防火墙服务 systemctl start firewalld # 关闭防火墙服务 systemctl stop firewalld # 查看防火墙服务状态 systemctl s ...
- 文心一言 VS 讯飞星火 VS chatgpt (103)-- 算法导论10.1 1题
一.用go语言,仿照图 10-1,画图表示依次执行操作 PUSH(S,4).PUSH(S,1).PUSH(S,3).POP(S).PUSH(S,8)和 POP(S)每一步的结果,栈 S初始为空,存储于 ...
- Rethinking Point Cloud Registration as Masking and Reconstruction论文阅读
Rethinking Point Cloud Registration as Masking and Reconstruction 2023 ICCV *Guangyan Chen, Meiling ...
- 从零用VitePress搭建博客教程(1) – VitePress的安装和运行
1.从零用VitePress搭建博客说明(1) – VitePress的安装和运行 一.写在前面 最近在想更新一把自己的前端吧小博客,但发现wordPress版本停留在了5年之前,发现变化挺大,不支持 ...
- MongoDB 中的锁分析
MongoDB 中的锁 前言 MongoDB 中锁的类型 锁的让渡释放 常见操作使用的锁类型 如果定位 MongoDB 中锁操作 1.查询运行超过20S 的请求 2.批量删除请求大于 20s 的请求 ...
- java_2.常量、Scanner类、基本数据类型转换、算数运算符
常量.Scanner类.基本数据类型转换.算数运算符 变量和常量 常量 1.使用final关键字修饰,只能赋值一次,不可以修改值. 2.常量的名称使用全大写. 3.多个单词使用下划线分隔. publi ...
- Go 接口-契约介绍
Go 接口-契约介绍 目录 Go 接口-契约介绍 一.接口基本介绍 1.1 接口类型介绍 1.2 为什么要使用接口 1.3 面向接口编程 1.4 接口的定义 二.空接口 2.1 空接口的定义 2.2 ...
- vivado生成Bitstream报错[Vivado 12-1345] Error(s) found during DRC. Bitgen not run(Vivado 2017.4)。
写了一个很简单的程序,2-4译码器. module decoder2to4( input in1, in0, output reg [3:0]out ); always @ (*) begin if ...
- 2021 ICPC济南 J Determinant
题意就是给定一个矩阵,然后给出他的行列式的绝对值,这个值是精确的,然后让我们判断行列式的正负. 思路来源:一个Acmer 首先做这个题要明白一个性质才可以做,一个数和它的相反数对一个奇数的取模一定不同 ...