How do i display the value in a tkinter MenuOption on selection of a row in a TreeView?

115 Views Asked by At

I have a tkinter GUI app connected to a MySQL database that uses a Treeview that displays, adds, deletes, and updates the records in an employee table using buttons. Below the Treeview I have several entry widgets and an OptionMenu widget (named post_menu) for data input. When I click on a row in the Treeview, the values are output to entry widgets. However, the OptionMenu widget does not update but instead shows the default value which is akin to placeholder text. My question is how do I get the OptionMenu to update its value similar to the entry widgets? For example, if I select a row to update a record the OptionMenu should show the corresponding value stored in the database for that record. To get the entry widgets to get populated with text from the selected row in the Treeview, I used the bind method of the Treeview class. I have also defined a select_reord() function that is passed to the bind method . Below is a snippet of the select_record() function.

def select_record(e):
    # clear entry boxes
    emp_id_entry.delete(0, END)
    salary_entry.delete(0, END)
    fn_entry.delete(0, END)
    ln_entry.delete(0, END)
    email_entry.delete(0, END)

    # Grab record number
    selected_record = employee_tree.focus()
    # Grab record values
    values = employee_tree.item(selected_record, 'values')

    # output to entry boxes
    emp_id_entry.insert(0, values[0])
    salary_entry.insert(0, values[1])
    fn_entry.insert(0, values[2])
    ln_entry.insert(0, values[3])
    **#post_menu.selection_get() # I need to display the value from the Treeview here**
    email_entry.insert(0, values[5]

# Bind the treeview
employee_tree.bind('<ButtonRelease-1>', select_record)

-----------------------------------------------------------------
# Add record entry boxes
employee_frame = ttk.LabelFrame(root, text='Employee Record')
employee_frame.pack(fill='x', expand='yes', padx=20)

emp_id_label = ttk.Label(employee_frame, text='Employee ID')
emp_id_label.grid(row=0, column=0, padx=10, pady=10, sticky=W)
emp_id_entry = ttk.Entry(employee_frame, width=10, style='My.TEntry')
emp_id_entry.focus()
emp_id_entry.grid(row=0, column=1, padx=5, pady=10, sticky=W)

salary_label = ttk.Label(employee_frame, text='Salary $')
salary_label.grid(row=0, column=2, padx=5, pady=10, sticky=E)
salary_entry = ttk.Entry(employee_frame, width=10, style='My.TEntry')
salary_entry.grid(row=0, column=3, padx=5, pady=10, sticky=W)

fn_label = ttk.Label(employee_frame, text='First Name')
fn_label.grid(row=1, column=0, padx=10, pady=10, sticky=W)
fn_entry = ttk.Entry(employee_frame, width=33, style='My.TEntry')
fn_entry.grid(row=1, column=1, padx=5, pady=10,sticky=W)

ln_label = ttk.Label(employee_frame, text='Last Name')
ln_label.grid(row=1, column=2, padx=5, pady=10, sticky=E)
ln_entry = ttk.Entry(employee_frame, width=40, style='My.TEntry')
ln_entry.grid(row=1, column=3, padx=5, pady=10, sticky=W)

post_label = ttk.Label(employee_frame, text='Post')
post_label.grid(row=2, column=0, padx=5, pady=10, sticky=E)

# Declare a list to store the items in the options menu
options = [
    '----Select Employee Post----',
    'Assistant Customs Officer I',
    'Assistant Customs Officer II',
    'Assistant Customs Officer III',
    'Assistant Chief Guard',
    'Chief Guard',
    'Customs Officer I',
    'Customs Officer II',
    'Customs Officer III',
    'Customs Inspector I',
    'Customs Inspector II',
    'Office Assistant',
    'Porter'
]

# Find the length of maximum character in the option
menu_width = len(max(options, key=len))
selected = StringVar(root)  # declare a variable to store the selected item in the option menu
selected.set(options[0])  # Set the first item in the list as default

post_menu = ttk.OptionMenu(employee_frame, selected, *options)
post_menu.config(width=menu_width)
post_menu.grid(row=2, column=1, padx=5, pady=10, sticky=W)

email_label = ttk.Label(employee_frame, text='Email')
email_label.grid(row=2, column=2, padx=5, pady=10, sticky=E)
email_entry = ttk.Entry(employee_frame, width=40, style='My.TEntry')
email_entry.grid(row=2, column=3, padx=5, pady=10, sticky=W)


0

There are 0 best solutions below