python tkinter库的Text记录点击路经和删除记录详情
作者:MAR-Sky 时间:2021-04-15 03:41:13
前言
需要注意,对实例化的文本组件的insert、delete等操作的index**都是浮点型而不是整型**,(1.0,2.0)表示的是对第一行操作,关闭窗口需要知道作用的对象是最根本的窗口,不是某个Frame。
Text的几个主要设置参数:
第一个参数:窗体或框架变量
state:控制是否可以修改text的文字内容,normal,disable
width,height:宽和高
text组件的一个主要操作:
txt_entry=Text(wintool,state,width,height).pack()
txt_entry.get(start_index,end_index)#读取text中的字符内容,start_index,end_index都是浮点数,整数部分表示一行
txt_entry.delete(start_index,end_index) # 删除文本
txt_entry.insert(start_index,'文本内容')# 对text插入文本内容,若是结尾索引可以使用END
import tkinter.filedialog as fd:用于读取文件或路径的库
fd.askdirectory:返回点击的路径
fd.askopenfilenames():返回多个选中文件的名字
txt_entry=Text(wintool,width,height,).pack():组件实例化和放置
txt_entry(组件实例化后的变量)
对点击打开的文件路径进行记录显示
from tkinter import *
import tkinter.filedialog as fd
all_path = [] # 记录全局路径
def openFloder():
folder_path = fd.askdirectory(initialdir=r"D:\graduate\小程序\车牌的总数量及省份统计") # 打开文件
show_folderPath.delete(0.0,END) # 清空
show_folderPath.insert(0.0,folder_path) #写入路径
print(folder_path)
all_path.append(folder_path)
txt.delete(0.0,'end')
txt.insert(0.0,all_path) #插入的第一个字符是索引马,不能是整数形式
print('all_path:',all_path)
root = Tk() # 生成主窗口对象
root.title('Demo') # 窗口标题
root.geometry('400x400') # 窗口大小
fr= Frame(root,width=200,height=200,)
fr.pack(side='top',expand='yes')
txt = Text(fr,bd=5)
txt.pack(side='bottom')
txt_txt = txt.get(0.0)
print('txt_txt:',txt_txt)
fr1= Frame(fr,width=100,height=30,bg='gray')
fr1.pack(side='left',expand='yes')
fr2= Frame(fr,width=100,height=30,bg='white')
fr2.pack(side='left',expand='yes')
show_folderPath = Entry(fr2)
show_folderPath .pack(side='left')
btn = Button(fr1,bg='orange',text ="选择文件",command = openFloder) # 在root窗口对象上设置一个按钮,用于打开文件和返回选择的文件名
btn.pack()
root.mainloop()
记录点击的文件路径和文件夹路径
更多相关文件路径可以参考
# from tkinter import *
import tkinter as tk
import tkinter.filedialog as fd
from tkinter import filedialog
def select_file():
# 单个文件选择
selected_file_path = filedialog.askopenfilename() # 使用askopenfilename函数选择单个文件
select_path.set(selected_file_path)
def select_files():
# 多个文件选择
selected_files_path = filedialog.askopenfilenames() # askopenfilenames函数选择多个文件
select_path.set('\n'.join(selected_files_path)) # 多个文件的路径用换行符隔开,更新tkinter的字符变量
def select_folder():
# 文件夹选择
selected_folder = filedialog.askdirectory() # 使用askdirectory函数选择文件夹
select_path.set(selected_folder)
root = tk.Tk()
root.title("选择文件或文件夹,得到路径")
# 初始化Entry控件的textvariable属性值,能够实时读取控件的变化
select_path = tk.StringVar()
# 布局控件
tk.Label(root, text="文件路径:").grid(column=0, row=0, rowspan=3)
tk.Entry(root, textvariable = select_path).grid(column=1, row=0, rowspan=7)
tk.Button(root, text="选择单个文件", command=select_file).grid(row=0, column=2)
tk.Button(root, text="选择多个文件", command=select_files).grid(row=1, column=2)
tk.Button(root, text="选择文件夹", command=select_folder).grid(row=2, column=2)
root.mainloop()
记录文件路径,在text中显示,删除和关闭窗口
from tkinter import *
import tkinter as tk
import tkinter.filedialog as fd
all_paths=[]
def real_close(): #定义关闭窗口命令,在button中使用,若使用定义的函数,必须把定义放在使用语句的前边
root.quit()
def choose_folder(): # 每次选择一个文件路径
folder_dirs["state"] = 'normal' # 将文本框组件修改为可修改
# folder_dirs.delete(0.0,END) # 清空,第一个参数必须是浮点型的不能是整型
select_folder = fd.askdirectory() # 每次点击文件
select_paths.set(select_folder) #为了显示
all_paths.append(select_folder) # 为了记录点击过的所有文件路径
print(all_paths)
# text组件对记录的路径显示,且连续从后边插入,为了显示
folder_dirs.insert('end',select_paths.get()+'\n')
folder_dirs["state"] = 'disable' # 将文本框修改为不可修改
def delete_folder(): # 每次选择一个文件路径
folder_dirs["state"] = 'normal' # 将文本框组件修改为可修改
folder_txt = folder_dirs.get(1.0,'end').split('\n')
txt_len = len(folder_txt)
txt_start = '%s.0'%(txt_len-2) # text组件中的索引表示的含义:1.1表示第一行第一个字符,
txt_end = '%s.0'%(txt_len-1)
# folder_dirs.delete(1.0,2.0)表示删除第一行内容
folder_dirs.delete(txt_start,txt_end)
all_paths.pop()
print(all_paths)
folder_dirs["state"] = 'disable' # 将文本框修改为不可修改
root=Tk()
root.title('文件夹的选择和路径显示')
root.geometry('800x400') # 窗体大小
# 记录点取的路径,使用内容时需要使用get()方法,每次记录一个路径
select_paths = tk.StringVar()
Label(root,text="folders:",font=("Arial", 16),bg='yellow').pack(side='left',padx=5,pady=5)
folder_dirs=Text(root,state='disable',bd=5,width=50,height=25,) # 定义路径文本框
folder_dirs.pack(side='left',padx=5,pady=5) # 放置文本组件
Button(root, text = "choose folder", command=choose_folder,font=("Arial", 12),bg='green').pack(side='left',padx=5,pady=5)
Button(root, text = "delete folder", command=delete_folder,font=("Arial", 12),bg='red').pack(side='left',padx=5,pady=5)
but=Button(root, text = "EXIT", command=real_close,font=("Arial", 12),bg='red')
but.pack(side='bottom',expand=0)
root.mainloop()
来源:https://blog.csdn.net/weixin_43794311/article/details/124682879
标签:python,tkinter,库,Text,路经,删除
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Thinkphp5.1获取项目根目录以及子目录路径的方法实例讲解
2023-11-20 00:55:58
python中用ggplot绘制画图实例讲解
2023-07-04 07:25:16
![](https://img.aspxhome.com/file/2023/1/77041_0s.jpg)
ASP程序种如何调用DLL文件
2008-01-15 19:12:00
深入浅析python的第三方库pandas
2021-06-05 03:13:03
![](https://img.aspxhome.com/file/2023/0/69010_0s.png)
Python实现的检测网站挂马程序
2023-11-21 16:39:38
ASP小偷(远程数据获取)程序的入门教程
2007-09-21 12:48:00
Python 多进程并发操作中进程池Pool的实例
2022-06-28 16:31:37
asp如何读取一个文件内容?
2009-11-19 17:23:00
Python+Pygame实战之24点游戏的实现
2023-11-07 21:14:12
![](https://img.aspxhome.com/file/2023/9/79469_0s.jpg)
跟老齐学Python之私有函数和专有方法
2021-04-13 20:38:18
Facebook:产品设计评价体系解密
2011-05-24 17:13:00
让IE8支持eWebEditor在线编辑器
2010-02-28 10:36:00
在oracle 数据库查询的select 查询字段中关联其他表的方法
2009-08-31 12:27:00
python rsync服务器之间文件夹同步脚本
2023-02-23 05:05:56
SQL Server数据库动态交叉表的参考示例
2009-01-04 14:44:00
Python标准库06之子进程 (subprocess包) 详解
2021-05-24 02:00:25
Javascript 中对中文长度对行判断
2009-07-05 18:39:00
Python+pytorch实现天气识别
2021-09-23 18:49:13
![](https://img.aspxhome.com/file/2023/6/79566_0s.png)
python基本语法练习实例
2021-02-25 06:50:07
如何不通过DSN访问SQL Server?
2009-11-11 19:21:00