[Python] Python / tkinter: Binding right button click in an array of widgets

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am rendering an array of buttons onto the screen and want to implement a right-click function. I have left click working with the default "command=" option on the widget, but for some reason, I can't seem to get the button bind to kick in. My code looks like this:

    for key, value in sorted_widget_properties:
    if key not in self._filter_list:
    continue
    colour = value[appearance_mode_index]
    if row > 18:
    offset = 4
    row = 1

    # Light mode colours
    if row == 1:
    pad_y = (10, 0)
    else:
    pad_y = 5
    lbl_property = ctk.CTkLabel(master=widget_frame, text=' ' + key)
    lbl_property.grid(row=row, column=1 + offset, sticky='w', pady=pad_y)
    btn_property = ctk.CTkButton(master=widget_frame,
    border_width=1,
    fg_color=colour,
    width=button_width,
    height=button_height,
    text='',
    command=lambda widget_property=key: self.colour_picker(widget_property),
    corner_radius=3)
    btn_property.grid(row=row, column=0 + offset, padx=5, pady=pad_y)
    self.widgets[key] = {"widget": btn_property, "button": btn_property, "colour": colour,
    'label': lbl_property}
    # Set a binding so that we can paste a colour, previously copied into our clipboard
    self.widgets[key]['widget'].bind("<Button-3>",
    lambda widget_property=key: self._paste_colour(widget_property))
    row += 1


    I have a print statement in the _paste_colour class method, and it appears that the function is never called and nothing is ever printed:

    def _paste_colour(self, widget_property):
    print('PASTE COLOUR!"')
    new_colour = pyperclip.paste()
    if len(new_colour) != 7:
    self._status_bar.set_status_text(status_text='Attempt to paste a bad colour code - ignored.')
    self._set_widget_colour(widget_property=widget_property, new_colour=new_colour)
    self._status_bar.set_status_text(
    status_text=f'Colour {new_colour} assigned to widget property {widget_property}.')

    Continue reading...

Compartilhe esta Página