How to be able to select the text in a tkinter Message widget?
Short answer is no, you can't. You might be able to do some clever workaround with event capturing but it's much more work than you might be anticipating.
The most likely way to implement this as you mentioned is just emulate the Message
look on an Entry
or Text
widget. An easy way is using ttk.Style
to copy the look and feel of the widgets under ttk
instead. However there's no Message
widget under ttk
, but Label
cuts pretty close:
import tkinter as tkfrom tkinter import ttkroot = tk.Tk()lbl = ttk.Label(root, text='foo')lbl.winfo_class()# 'TLabel'# This means ttk.Label uses a 'TLabel' style# Set a StringVar to update the message without updating the statemy_txt = tk.StringVar()my_msg = ttk.Entry(root, text=my_txt, style='TLabel', justify='center', state='readonly')# justify to emulate the Message look (centered).# state = `readonly` to protect the Entry from being overwrittenmy_txt.set('message here')
Your Entry
widget will now look like a Message
widget with the text 'message here'
to copy without write access.
Edit: If you want to resize the entry based on the characters, assuming you have a fixed-length font, you can try this:
my_msg.pack(expand=True, fill=tk.BOTH)my_txt.set('this is a way longer message so give it a try whatever')my_msg.configure(width=len(my_txt.get()))
If your font is not fixed-length you can guestimate an average/max width per character increase and multiply the ratio to the len()
:
my_msg.configure(width=int(len(my_txt.get())*0.85))# where you anticipate each character might take only up to 85% of the normal character width