In python/tkinter, is it possible to change a access db result name before it hits in insert in treeview?

75 views Asked by At

I am using python/tkinter. I am connected to a mdb and querying it for employees where one column(supervisor) is checked or unchecked depending on if they are one. Checked = Yes is a supervisor, unchecked is No, not a supervisor. In my treeview insert for this, checked appears as 'True', unchecked appears as 'False'. I would ideally want True to be 'Y' and False to be 'N'. Is this possible? I have tried many things like if else, changing the query to the mdb, and others with no luck. Any and all suggestions greatly appreciated. Thanks.

Code snippet below:

#CONNECT TO MDB CASHIERS
    conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=<pathway removed>;')
    cursor = conn.cursor()    
    
    cashiers = cursor.execute("SELECT * from users WHERE active ORDER BY fullname")
    data = cursor.fetchall()
    
    conn.commit()
    
    conn.close()
    
    style = ttk.Style()

    style.theme_use('default')

    style.configure("Treeview",
        background='lightblue',
        foreground='black',
        rowheight=30,
        fieldbackground='#b568d9')
    
    style.map('Treeview',
        background=[('selected', "#347083")])
    
    tree_frame = Frame(casrwin)
    tree_frame.pack(pady=10)

    tree_scroll = Scrollbar(tree_frame)
    tree_scroll.pack(side=RIGHT, fill=Y)

    my_tree = ttk.Treeview(tree_frame, yscrollcommand=tree_scroll.set, selectmode="extended")
    my_tree.pack()

    tree_scroll.config(command=my_tree.yview)

#COLUMNHEADINGS
    my_tree['columns'] = ("User", "Full Name", "Location", "Drawer 1", "Drawer 2", "Drawer 3", "Supervisor")

    my_tree.column("#0", width=0, stretch=NO)
    my_tree.column("User", anchor=W, width=140)
    my_tree.column("Full Name", anchor=W, width=140)
    my_tree.column("Location", anchor=W, width=175)
    my_tree.column("Drawer 1", anchor=W, width=140)
    my_tree.column("Drawer 2", anchor=W, width=140)
    my_tree.column("Drawer 3", anchor=W, width=140)
    my_tree.column("Supervisor", anchor=W, width=140)

    my_tree.heading("#0", text="", anchor=W)
    my_tree.heading("User", text="User", anchor=W)
    my_tree.heading("Full Name", text="Full Name", anchor=W)
    my_tree.heading("Location", text="Location", anchor=W)
    my_tree.heading("Drawer 1", text="Drawer 1", anchor=W)
    my_tree.heading("Drawer 2", text="Drawer 2", anchor=W)
    my_tree.heading("Drawer 3", text="Drawer 3", anchor=W)
    my_tree.heading("Supervisor", text="Supervisor", anchor=W)


    my_tree.tag_configure('blue', background="lightblue")
    my_tree.tag_configure('normal', background="white")

    my_tag='normal'

#ADD DATA TO SCREEN
    for record in data:
        my_tag='gray' if my_tag=='normal' else 'normal'
        my_tree.insert(parent='', index='end', iid=record[0], text='', values=(record[0], record[3], record[1], record[9], record[10], record[11], record[5], record[4], record[6], record[7], record[8]), tags=(my_tag))

I tried if else, changing mdb query, all in many different places and iterations without success.

1

There are 1 answers

2
An0nymous On BEST ANSWER

To display 'Y' for True and 'N' for False in your Treeview column for supervisors, you can modify the data you're inserting into the Treeview. You can use a conditional statement to convert True to 'Y' and False to 'N' before inserting it into the Treeview.

To do that, change the last part of the code which says # ADD DATA TO SCREEN to the code below.

# ADD DATA TO SCREEN
for record in data:
    my_tag = 'gray' if my_tag == 'normal' else 'normal'
    supervisor_value = 'Y' if record[5] else 'N'  # Convert True to 'Y' and False to 'N'
    my_tree.insert(parent='', index='end', iid=record[0], text='', values=(record[0], record[3], record[1], record[9], record[10], record[11], supervisor_value, record[4], record[6], record[7], record[8]), tags=(my_tag))