Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Tkinter] Select All text without space on the righthand side
Hello, please how can I select all the text in Text Wigdet just like that on Microsoft windows Notepad without including unnecessarily space on the right hand side for each line in the widget. Sorry just in case you don't get me, please try opening Notepad on windows Os and try to select all the text in it.

from tkinter import *
from tkinter import ttk

class Notepad:
    def __init__(self):
      self.master = Tk()

    def build_Notepad(self):
        self.frame_2 = ttk.Frame(self.master)
        self.frame_2.pack(fill=BOTH, expand=True)
        self.scrollbar = ttk.Scrollbar(self.frame_2)
        self.scrollbar.pack(side=RIGHT, fill=Y, expand=False)
        self.scrollbar_2 = ttk.Scrollbar(self.master, orient=HORIZONTAL)
        self.scrollbar_2.pack(fill=X, expand=NO)     
        self.text = Text(self.frame_2,yscrollcommand=self.scrollbar.set,xscrollcommand=self.scrollbar_2.set,wrap=NONE,undo=True)
        self.text.insert(INSERT,'Select every text now')

    def select_all_text(self,event=None):
        for self.line in self.text.get('1.0','end-1c').splitlines():
                self.line = self.text
                self.line.tag_add(SEL,'insert linestart','insert lineend')
        return 'break'

if __name__ == '__main__':
The ability to select the code is built into the Text widget, but this is not a trivial widget as it contains methods that will allow you to actually reproduce the functions you would find in MS word.

So you will need to mark and select the block of text and do what you want to do with it.
The best freely available write-up on this that I have found is Shipman's reference manual, though old, it's still 100% valid as tkinter hasn't changed much. You may want to download the complete reference manual PDF here:
see the text write-up here:
especially the part about selecting text that begins here:

There is a book that actually has a chapter (# 2) dedicated to creating an editor that contains all of the editor functionality that you would need to consider it a useful editor. That book is Tkinter hotshot, and is available here: for $13.50 U.S.
I would recommend this book just based on the contents of chapter 2.
It's compact, not overly comprehensive, and get's to the point.
(I have nothing to do with writing this book, nor the publisher, this is my opinion only)

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Update value selected in option menu when select an item from text box klllmmm 2 1,344 Jun-06-2019, 04:51 AM
Last Post: klllmmm

Forum Jump:

Users browsing this thread: 1 Guest(s)