[Python] Python Tkinter how to set and highlight a listbox selection

Discussão em 'Python' iniciado por Stack, Dezembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    I have a listbox which I want to set according to data from elsewhere in my program

    options = tk.Variable(('Active', 'Inactive', 'OOC'))
    ttk.Label(
    self,
    text='Status',
    ).grid(row=4, column=0, sticky='ne', padx=5)

    self.widgets['status'] = tk.Listbox(
    self,
    listvariable=options,
    height=3,
    selectmode=tk.SINGLE,
    )

    self.widgets['status'].grid(row=4, column=1, sticky='ew', padx=(0, 5), columnspan=4)

    if data:
    self.widgets['status'].selection_clear(0, tk.END)
    self.widgets['status'].selection_set(data[4])
    self.widgets['status'].activate(data[4])


    The code above works in that it sets the listbox to the correct item - if I tab to the listbox, the item is surrounded by a dotted box - but does not highlight it. After a lot of searching, the only suggested solution I've found is to add self.widgets['status'].event_generate('<<ListboxSelect>>') but this doesn't work for me. How do I get the selected item highlighted?

    Continue reading...

Compartilhe esta Página