SoFunction
Updated on 2024-11-14

The python tkinter library's Text logging click-through routing and deletion of logging details

preamble

Note that the index ** of the instantiated text component insert, delete and other operations are floating-point rather than integer **, (1.0, 2.0) represents the first line of the operation, closing the window need to know the role of the object is the most fundamental window, not a Frame.

There are a few key setup parameters for Text:

  • First parameter: form or frame variable
  • state: control whether you can modify the text of the text content, normal, disable
  • width,height: width and height

A major operation of the text component:

txt_entry=Text(wintool,state,width,height).pack()
txt_entry.get(start_index,end_index)# Read the content of the characters in the text, start_index, end_index are floating point numbers, the integer part of a line
txt_entry.delete(start_index,end_index)  # Delete text
txt_entry.insert(start_index,'Text content')# treat (sb a certain way)textInsert text content,If the index is a closing index you can use theEND
import  as fd:Libraries for reading files or paths
:Returns the path of the click
():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 the path of the file opened by clicking on it.

from tkinter import *
import  as fd
all_path = []  # Record global paths
def openFloder():
    folder_path = (initialdir=r"D:\graduate\ small program\ license plate total number and province statistics") # Open the file
    show_folderPath.delete(0.0,END)  # Clear
    show_folderPath.insert(0.0,folder_path)  # Write path
    print(folder_path)
    all_path.append(folder_path) 
    (0.0,'end')
    (0.0,all_path)  # The first character inserted is the index horse and cannot be in integer form
    print('all_path:',all_path)
    root = Tk()   # Generate main window object
('Demo') # Window title
('400x400') # Window size
fr= Frame(root,width=200,height=200,)
(side='top',expand='yes')
txt = Text(fr,bd=5)
(side='bottom')
txt_txt = (0.0)
print('txt_txt:',txt_txt)
fr1= Frame(fr,width=100,height=30,bg='gray')
(side='left',expand='yes')
fr2= Frame(fr,width=100,height=30,bg='white')
(side='left',expand='yes')
show_folderPath = Entry(fr2)
show_folderPath .pack(side='left')

btn = Button(fr1,bg='orange',text ="Selection of documents",command = openFloder) # Set a button on the root window object to open the file and return the selected file name
()
() 

Record clicked file paths and folder paths

More relevant file paths can beconsultation

# from tkinter import *
import tkinter as tk
import  as fd
from tkinter import filedialog

def select_file():
    # Individual document selection
    selected_file_path = ()  # Use the askopenfilename function to select a single file
    select_path.set(selected_file_path)  

def select_files():
    # Multiple document selection
    selected_files_path = ()  # askopenfilenames function to select multiple files
    select_path.set('\n'.join(selected_files_path))  # Paths to multiple files separated by newlines, update tkinter's character variables
def select_folder():
    # Folder selection
    selected_folder = ()  # Use the askdirectory function to select a folder
    select_path.set(selected_folder)
root = ()
("Select the file or folder to get the path")
# Initialize the value of the textvariable property of the Entry control to be able to read changes to the control in real time.
select_path = ()

# Layout controls
(root, text="File path:").grid(column=0, row=0, rowspan=3)
(root, textvariable = select_path).grid(column=1, row=0, rowspan=7)
(root, text="Selection of individual documents", command=select_file).grid(row=0, column=2)
(root, text="Select multiple files", command=select_files).grid(row=1, column=2)
(root, text="Select folder", command=select_folder).grid(row=2, column=2)
()

Record file paths, display in text, delete and close windows

from tkinter import *
import tkinter as tk
import  as fd
all_paths=[]
def real_close():  # Define a command to close the window, use it in the button, if you use a defined function, you must put the definition in front of the use statement.
    ()
def choose_folder():  # Select one file path at a time
    folder_dirs["state"] = 'normal'  # Modify the text box component to be modifiable
    # folder_dirs.delete(0.0,END) # empty, the first parameter must be floating point not integer
    select_folder = ()  # Every time you click on a file
    select_paths.set(select_folder)   # To show
    all_paths.append(select_folder)  # To record the paths of all the files clicked on
    print(all_paths)
    # The text component displays the path of the record and inserts it consecutively from the back, in order to display the
    folder_dirs.insert('end',select_paths.get()+'\n')  
    folder_dirs["state"] = 'disable'  # Change the text box to unchangeable
def delete_folder():  # Select one file path at a time
    folder_dirs["state"] = 'normal'  # Modify the text box component to be modifiable
    folder_txt = folder_dirs.get(1.0,'end').split('\n')
    txt_len = len(folder_txt)
    txt_start = '%s.0'%(txt_len-2)  # Meaning of the index representation in the text component: 1.1 means the first character of the first line.
    txt_end = '%s.0'%(txt_len-1) 
    # folder_dirs.delete(1.0,2.0) means delete the first line of content
    folder_dirs.delete(txt_start,txt_end)  
    all_paths.pop()
    print(all_paths)
    folder_dirs["state"] = 'disable'  # Change the text box to unchangeable
root=Tk()
('Folder selection and path display')
('800x400') # Form size
# Record the path of the point to take, the use of content need to use the get () method, each time to record a path
select_paths = () 
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 the path text box
folder_dirs.pack(side='left',padx=5,pady=5)  # Placement of text components
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')
(side='bottom',expand=0)
()

to this article on the python tkinter library Text record click through and delete the record details of the article is introduced to this, more related python tkinter library content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!