Python使用文件选择对话框

Python使用文件选择对话框

本文档记录Python使用文件选择对话框的一种方法。使用tkinter库。

1 基本操作

1.1 窗口的基础设置

import tkinter as tk
root = tk.Tk()		# 实例化窗口
root.title('My Window')
window.geometry('500x300')

root.mainloop()

1.2 获取目录(文件、文件夹)

import tkinter as tk			# 引入对应库
from tkinter import filedialog

root = tk.Tk()		# 创建对象


file_path = filedialog.askopenfilename()		# 选择文件,路径保存于file_path中
			# filedialog.askopenfilenames() 多个文件
foldr_patch = filedialog.askdirectory()			# 选择文件夹,路径保存于foldr_patch中
save_patch = filedialog.asksaveasfilename()		# 获取保存路径的目录

print(file_path)			# 打印 
print(foldrname_patch)

root.mainloop()

2 其他部件

2.1 显示信息在对象中

2.1.1 显示文本

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()

l = tk.Label(root, text='显示', bg='green', font=('Arial', 12), width=30, height=2)
# bg为背景,font为字体,width为长,height为高,这里的长和高是字符的长和高,比如height=2,就是标签有2个字符这么高
l.pack()    # Label内容content区域放置位置,自动调节尺寸

root.mainloop()

在这里插入图片描述

2.1.2 显示类似可以选择的列表

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()

listb  = tk.Listbox(root)		# 输出信息用
listb.insert(5, 2)		# 第一个数字的作用是选择后面信息插入位置,大于目前拥有信息,默认为最后一个
listb.insert(2, 3)
listb.insert(1, 4)

listb.pack()			# 打包放置到主窗口

root.mainloop()

Python使用文件选择对话框_第1张图片

2.2 按键

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()

def demo():
    print('输出内容!!')

b = tk.Button(root, text='look', font=('Arial', 12), width=10, height=1, command=demo)
					# 显示文字		字体设置				按键大小				触发事件
b.pack()	# 默认居中堆叠
# b.place(x=120, y=120)		# 选择一个位置放置,两个只能起作用一个

root.mainloop()

2.3 文本输入

2.3.1 单行文本获取

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()

e1 = tk.Entry(root, show='*', font=('Arial', 14))   # 显示show内的字符,可以实现加密之类的功能
e2 = tk.Entry(root, show=None, font=('Arial', 14))  # 显示成明文形式,输入什么显示什么
# textvariable显示想预先输入的文本
e1.pack()
e2.pack()

# 加入按键来获取输入文本
def demo():
    print(e1.get())
    print(e2.get())
    
b = tk.Button(root, text='look', font=('Arial', 12), width=10, height=1, command=demo)
b.pack()

root.mainloop()

Python使用文件选择对话框_第2张图片

2.3.2 多行文本获取

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()

t = tk.Text(root, height=3)
t.pack()

def demo():
    print(t.get(1.0, 'end'))    # 输出文本框内所有文本,1.0表示从第一行第一例开始读取,'end'表示读取到最后
    print(t.get(1.0, 2.3))
    
b = tk.Button(root, text='look', font=('Arial', 12), width=10, height=1, command=demo)
b.pack()

root.mainloop()

Python使用文件选择对话框_第3张图片
2.4 信息窗口弹出
2.4.1 无返回信息窗口

import tkinter as tk
from tkinter import messagebox    # 必须这样才可以,否则是not defined

root = tk.Tk()

t = tk.Text(root, height=3)
t.pack()

def demo():
    messagebox.showinfo(title='提示信息', message='蓝色感叹号')
    messagebox.showwarning(title='提示信息', message='黄色感叹号警告')
    messagebox.showerror(title='提示信息', message='红色叉叉错误')
    
b = tk.Button(root, text='look', font=('Arial', 12), width=10, height=1, command=demo)
b.pack()

root.mainloop()

Python使用文件选择对话框_第4张图片Python使用文件选择对话框_第5张图片Python使用文件选择对话框_第6张图片
2.4.2 带返回选择信息

import tkinter as tk
from tkinter import messagebox    # 必须这样才可以,否则是not defined

root = tk.Tk()

t = tk.Text(root, height=3)
t.pack()

def demo():
    print(messagebox.askquestion(title='提示信息', message='return \'yes\', \'no\'')))    # return 'yes', 'no'
    #print(messagebox.askyesno(title='提示信息', message='return \'True\', \'False\''))     # return 'True', 'False'
    #print(messagebox.askokcancel(title='提示信息', message='return \'True\', \'False\''))  # return 'True', 'False'
    ## title左上角标题,message中间显示文字

b = tk.Button(root, text='look', font=('Arial', 12), width=10, height=1, command=demo)
b.pack()

root.mainloop()

你可能感兴趣的:(#,Python文件处理,Python,python)