I want to create an object akin to Label, however I want it to also be selectable for copy-pasting. I have done so using the Text widget:
class CopyLabel (Text):
def __init__ (self, master, text = '', font = None):
if font is None:
super().__init__(master = master, height = 1,
borderwidth = 0, width = len(text),
bg = master['background'])
else:
super().__init__(master = master, height = 1,
borderwidth = 0, font = font,
width = len(text), bg = master['background'])
self.insert(1.0, text)
self.configure(state = 'disabled')
I end up displaying this widget with a grid. However, I randomly find the last 1 or 2 characters not showing. When investigating this, it seems the Text widget is splitting those characters off to a new line (when selecting the text, it is possible to drag down to see this second line). The biggest problem is the unpredictable nature of this splitting (I tried doing width = len(text) + 2, but I still occasionally get this line splitting behaviour). Is there any way to remedy this behaviour?
EDIT: setting wrap = 'none' fixed the line splitting behaviour, but the text is still getting cutoff. Now I have to scroll horizontally instead of vertically to see the text, but I guess that is a different question from what I posed.
The Entry widget is used to accept single-line text strings from a user. If you want to display multiple lines of text that can be edited, then you should use the Text widget.
We can use place() method to set the position of the Tkinter labels.
A text widget provides a multi-line text area for the user. The text widget instance is created with the help of the text class. It is also used to display text lines and also allows editing the text.
An Entry widget in Tkinter is nothing but an input widget that accepts single-line user input in a text field. To return the data entered in an Entry widget, we have to use the get() method. It returns the data of the entry widget which further can be printed on the console.
What you're experiencing is called wrapping and can be disabled modifying Text widget's wrap
option, as in :
self['wrap'] = 'none'
or
self.config(wrap='none')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With