有什么方法可以审查 Treeview 数据显示吗?

Any way to censor Treeview Data Display?

我可以使用 SQLite3 作为数据库 (DB) 并获取 Treeview 来显示数据库中的数据。但是,我想知道 Treeview 是否有任何功能可以为所有条目审查特定列中的前几个字符?

精简代码如下:

############################## | DATABASE CONNECTION FOR PARTICULARS | ####################################

connectforce = sqlite3.connect('force_database.db')

#Create Cursor - Link to database
c = connectforce.cursor()

##create table
#c.execute("""create table patient_list (
#    nric text, 
#    full_name text,
#    age integer,
#    gender text,
#    ethnic text
#    )""")


############################## | TREEVIEW CONNECTION | ####################################

my_tree = ttk.Treeview(frame4_pl)
my_tree['column'] = ("NRIC", "Full Name", "Age", "Gender", "Ethnic")
my_tree.column("#0", width=0, minwidth=25)
my_tree.column("NRIC", anchor=CENTER, width=90, minwidth=25)
my_tree.column("Full Name", anchor=W, width=150, minwidth=25)
my_tree.column("Age",anchor=CENTER, width=55, minwidth=25)
my_tree.column("Gender", anchor=CENTER, width=60, minwidth=25)
my_tree.column("Ethnic", anchor=CENTER, width=90, minwidth=25)

my_tree.heading("#0", text="", anchor=CENTER)
my_tree.heading("NRIC", text="NRIC", anchor=CENTER)
my_tree.heading("Full Name", text="Full Name", anchor=CENTER)
my_tree.heading("Age", text="Age", anchor=CENTER)
my_tree.heading("Gender", text="Gender", anchor=CENTER)
my_tree.heading("Ethnic", text="Ethnicity", anchor=CENTER)

my_tree.bind('<Double 1>', display_selection)

def submit_database():

    connectpatient = sqlite3.connect('patient_database.db')

    #Create Cursor - Link to database
    c = connectpatient.cursor()

    #Insert Into Table
    c.execute("INSERT INTO patient_list VALUES (:entry_nric, :entry_name, :entry_age, :entry_gender, :entry_Ethnic)",
              {
                'entry_nric': entry_nric.get(),
                'entry_name': entry_name.get(),
                'entry_age': entry_age.get(),
                'entry_gender': entry_gender.get(),
                'entry_Ethnic': entry_Ethnic.get()
              } )

    #Save into Latest/Selected Patient Array for 7TH FRAME
    latestpatient_list(1)

    #Commit Changes
    connectpatient.commit()

    #Close Connection
    connectpatient.close()

def view_database():
    connectpatient = sqlite3.connect('patient_database.db')

    #Create Cursor - Link to database
    c = connectpatient.cursor()
    
    #Query the Database
    c.execute("SELECT *, oid FROM patient_list")
    #c.execute("SELECT nric, full_name FROM patient_list")
    #c.execute("SELECT nric, full_name, age, gender, ethnic FROM patient_list WHERE nric LIKE '%"+keyword_check_passed+"%'")
    #c.execute("SELECT * FROM patient_list")
    records = c.fetchall()

    record = ""
    for record in records:
        print(record) # it print all records in the database
        my_tree.insert("", tk.END, values=record)                      #Perhaps some amendment here to censor with asterisk for some letters for "nric" column?
        my_tree.pack()

    #Commit Changes
    connectpatient.commit()

    #Close Connection
    connectpatient.close()

我可以从 Entry Widget (with Tkinter) 获取变量,以便在 view_database() 函数调用时成功存储并从更新的数据库反映到 Treeview。但我希望在查看 Treeview 时用 * 审查 nric 列数据的前 5 个字符。这是可能的还是可以使用的替代方法?

您可以在 SQL 查询本身中进行替换,方法是将所需的前缀与列的子字符串相结合,从第六个字符到末尾。

这是一个纯 Sqlite 示例:

sqlite> CREATE TABLE test (col1 text, col2 text);
sqlite> INSERT INTO test (col1, col2) VALUES ('Hello world', 'hello hello'), 
        ('First column', 'Second column');


sqlite> SELECT ('*****' || substr(col1, 6, length(col1))) AS redacted,
        col2 
        FROM test;
***** world|hello hello
***** column|Second column

substr is a function that takes a substring of a string value, || is the string concatenation operator.

AS redacted 标记新值将使 Sqlite 返回名为“redacted”的列中的值。

采用这种方法,view_database 函数可能如下所示:

def view_database():
    connectpatient = sqlite3.connect('patient_database.db')

    #Create Cursor - Link to database
    c = connectpatient.cursor()
    
    #Query the Database
    query = """SELECT '*****' || SUBSTR(nric, 6, LENGTH(nric)) AS nric,
               full_name, age, gender, ethnic, oid
               FROM patient_list"""

    c.execute(query)
    records = c.fetchall()

    record = ""
    for record in records:
        print(record) # it print all records in the database
        my_tree.insert("", tk.END, values=record)                      
        my_tree.pack()

    #Commit Changes
    connectpatient.commit()

    #Close Connection
    connectpatient.close()