How to be able to select the text in a tkinter Message widget?

2.4k Views Asked by At

I have a nice design for my GUI. I would want to be able to select the text displayed in Message widgets. The only suggestions I saw are to either use an Entry widget in the read only mode, but this looks completely different than a Message widget or to use a Text widget which again looks completely different. How can I select the text in a Message widget?

If this is not possible, how would I get a Text or Entry widget to look and behave the same as a Message widget?

1

There are 1 best solutions below

2
On

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 tk
from tkinter import ttk

root = 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 state
my_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 overwritten

my_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