I was looking around the web for a "selectable label" in tkinter, everyone seems to point to a disabled text entry. I wanted to share mine thus far. I plan to modify this so that when the label is selected, you can click an "edit" button elsewhere in the program and then do something to the label.
import tkinter as tk
class SelectableLabel(tk.Label):
def __init__(self, parent, text="", bg="white", fg="black"):
tk.Label.__init__(self, parent)
self.text = text
self.bg = bg
self.fg = fg
self.config(text=text, bg=bg, fg=fg)
self.bind('<Enter>', lambda x: self._label_enter())
self.bind('<Leave>', lambda x: self._label_leave())
self.bind('<Button-1>', lambda x: self._label_action())
@staticmethod
def _label_enter():
label.config(bg="black", fg="white")
@staticmethod
def _label_leave():
label.config(bg="white", fg="black")
@staticmethod
def _label_action():
print("Label Selected")
root = tk.Tk()
root.geometry("400x400")
label = SelectableLabel(root, text="Test Label")
label.pack()
root.mainloop()