How do I get Treeview in tkinter to show NULL as blank instead of "none"

56 views Asked by At

I am pulling in data from a database and putting it into a treeview. However, the treeview is displaying NULL values as "none" instead of blank. When I look at the database the null values are blank (null in very light gray in the background). The database I'm using is SQLite3. I'm using VS Code and the Gitbash terminal from within VS Code. The problem with this is that I'm using the Docxtemplate.render function to populate the data onto a word diploma template, but it's putting "none" in the concentration field if students don't have a concentration. I just need it to be blank. Here is the generate function. "diploma" is my variable for my Docxtemplate instantiated class. PS...I know there is a better way to do this than to use global variables and then create them then put them in the dictionary. I'll fix that issue later.

def generate():
    global studentid
    global fullName
    global degreeType
    global major1
    global concentration
    global gradDate
    global honors

    studentid = student_id_entry.get()
    fullName = full_name_entry.get()
    degreeType = degree_1_entry.get()
    major1 = major_1_entry.get()
    concentration = concentration_entry.get()
    gradDate = grad_date_entry.get()
    honors=honor_1_entry.get()

    diploma.render({
        "fullName" : fullName,
        "degreeType" : degreeType,
        "major": major1,
        "concentration": concentration,
        "honors": honors,
        "graduationDate" : gradDate
        })
    diploma.save(f"{studentid} {fullName} {degreeType} {major1}.docx") #composite primay key

    messagebox.showinfo("Success!", "The students diploma has been created!")

Here is the code to create the treeview:

# Create The Treeview
my_tree = ttk.Treeview(tree_frame, yscrollcommand=tree_scroll.set, 
    xscrollcommand=tree_scroll_bottom.set, selectmode="extended", show="headings")
    my_tree.pack()

Here is the function that populates the treeview:

def query_database():
    # Clear the Treeview
    for record in my_tree.get_children():
        my_tree.delete(record)
        
    # Create a database or connect to one that exists
    conn = sqlite3.connect(db)

    # Create a cursor instance
    c = conn.cursor()

    c.execute("SELECT rowid, * FROM graduates")
    records = c.fetchall()
    
    # Add our data to the screen
    global count
    count = 0
    
    #for record in records:
    #   print(record)

    for record in records:
        if count % 2 == 0:
            my_tree.insert(
                parent='', index='end', iid=count, text='', values=( record[1], record[2],
                record[3], record[4], record[5], record[6], record[7], record[8], record[9],
                record[10], record[11], record[12]), tags=('evenrow',))
        else:
            my_tree.insert(parent='', index='end', iid=count, text='', values=( record[1], record[2], record[3], record[4], record[5], record[6], record[7], record[8], record[9], record[10], record[11], record[12]), tags=('oddrow',))
        # increment counter
        count += 1


    # Commit changes
    conn.commit()

    # Close our connection
    conn.close()

Any help would be much appreciated.

1

There are 1 answers

0
Edo Akse On

To turn the comments into an actual answer:

Basically you can do a conditional when setting the value for concentration to simply turn the value "none" into and empty string ""

concentration = concentration_entry.get() if concentration_entry.get() != "none" else ""