Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force tkinter text widget to stay on one line

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.

like image 486
Yaroslav Avatar asked Dec 13 '17 12:12

Yaroslav


People also ask

Which widget is used in tkinter single line input?

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.

How do I change the position of text in tkinter?

We can use place() method to set the position of the Tkinter labels.

Which widget is used for multi-line text field?

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.

What does get () do in tkinter?

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.


1 Answers

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')
like image 110
Nae Avatar answered Sep 27 '22 20:09

Nae