Preface
Record and display the file path opened by clicking
Record the file path and folder path clicked
Record file path , stay text It shows that , Delete and close windows
PrefaceWe need to pay attention to , Of instantiated text components insert、delete The operations such as index** Floating point type, not integer type **,(1.0,2.0) Indicates the operation on the first line , To close a window, you need to know that the object of action is the most fundamental window , Not a certain one. Frame.
Text Several main setting parameters :
The first parameter : Form or frame variable
state: Controls whether you can modify text Text content ,normal,disable
width,height: Width and height
text A major component of :
txt_entry=Text(wintool,state,width,height).pack()txt_entry.get(start_index,end_index)# Read text The character content in ,start_index,end_index All floating point numbers , The integer part represents a line txt_entry.delete(start_index,end_index) # Delete text txt_entry.insert(start_index,' Text content ')# Yes text Insert text content , If it is the end index, you can use END
import tkinter.filedialog as fd: Library for reading files or paths fd.askdirectory: Return to the clicked path fd.askopenfilenames(): Returns the names of multiple selected files txt_entry=Text(wintool,width,height,).pack(): Component instantiation and placement txt_entry( Variables after component instantiation )
Record and display the file path opened by clicking from tkinter import *import tkinter.filedialog as fdall_path = [] # Record global path def openFloder(): folder_path = fd.askdirectory(initialdir=r"D:\graduate\ Applet \ Total number of license plates and statistics of provinces ") # Open file show_folderPath.delete(0.0,END) # Empty show_folderPath.insert(0.0,folder_path) # Write path print(folder_path) all_path.append(folder_path) txt.delete(0.0,'end') txt.insert(0.0,all_path) # The first character inserted is the index , Cannot be in integer form print('all_path:',all_path) root = Tk() # Generate the main window object root.title('Demo') # Window title root.geometry('400x400') # Window size 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 =" Select File ",command = openFloder) # stay root Set a button on the window object , Used to open the file and return the selected file name btn.pack()root.mainloop()
Record the file path and folder path clicked For more relevant file paths, please refer to
# from tkinter import *import tkinter as tkimport tkinter.filedialog as fdfrom tkinter import filedialogdef select_file(): # Single file selection selected_file_path = filedialog.askopenfilename() # Use askopenfilename Function to select a single file select_path.set(selected_file_path) def select_files(): # Multiple file selections selected_files_path = filedialog.askopenfilenames() # askopenfilenames Function to select multiple files select_path.set('\n'.join(selected_files_path)) # The paths of multiple files are separated by newline characters , to update tkinter Character variable def select_folder(): # Folder selection selected_folder = filedialog.askdirectory() # Use askdirectory Function to select a folder select_path.set(selected_folder)root = tk.Tk()root.title(" Select a file or folder , Get the path ")# initialization Entry The control of textvariable Property value , Be able to read the changes of the control in real time select_path = tk.StringVar()# Layout control tk.Label(root, text=" File path :").grid(column=0, row=0, rowspan=3)tk.Entry(root, textvariable = select_path).grid(column=1, row=0, rowspan=7)tk.Button(root, text=" Select a single file ", command=select_file).grid(row=0, column=2)tk.Button(root, text=" Select multiple files ", command=select_files).grid(row=1, column=2)tk.Button(root, text=" Select the folder ", command=select_folder).grid(row=2, column=2)root.mainloop()
Record file path , stay text It shows that , Delete and close windows from tkinter import *import tkinter as tkimport tkinter.filedialog as fdall_paths=[]def real_close(): # Define the close window command , stay button Use in , If you use a defined function , The definition must be placed before the use statement root.quit()def choose_folder(): # Select one file path at a time folder_dirs["state"] = 'normal' # Change the text box component to modifiable # folder_dirs.delete(0.0,END) # Empty , The first parameter must be floating-point, not integer select_folder = fd.askdirectory() # Every time you click on a file select_paths.set(select_folder) # To show all_paths.append(select_folder) # In order to record all the file paths clicked print(all_paths) # text The component displays the path of the record , And continuously insert... From the back , To show folder_dirs.insert('end',select_paths.get()+'\n') folder_dirs["state"] = 'disable' # Change the text box to non modifiable def delete_folder(): # Select one file path at a time folder_dirs["state"] = 'normal' # Change the text box component to modifiable folder_txt = folder_dirs.get(1.0,'end').split('\n') txt_len = len(folder_txt) txt_start = '%s.0'%(txt_len-2) # text The meaning of the index in the component :1.1 Represents the first character of the first line , txt_end = '%s.0'%(txt_len-1) # folder_dirs.delete(1.0,2.0) Means to delete the first line folder_dirs.delete(txt_start,txt_end) all_paths.pop() print(all_paths) folder_dirs["state"] = 'disable' # Change the text box to non modifiable root=Tk()root.title(' Folder selection and path display ')root.geometry('800x400') # Form size # Record the path taken by the point , When using content, you need to use get() Method , Record one path at a time 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,) # Define path text box folder_dirs.pack(side='left',padx=5,pady=5) # Place text component 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()
This is about python tkinter Library Text This is the end of the article about recording the click path and deleting the record details , More about python tkinter library Please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !