Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Tkinter] Select All text without space on the righthand side
#1
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.
Thanks.

from tkinter import *
from tkinter import ttk

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

    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.pack(side=LEFT,fill=BOTH,expand=True)
        self.text.bind('<Button-3>',self.select_all_text)
        self.scrollbar.configure(command=self.text.yview)
        self.scrollbar_2.configure(command=self.text.xview)
        self.text.insert(INSERT,'Select every text now')

    def select_all_text(self,event=None):
        self.text.tag_configure('sel',background='#8A2BE2')
        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')
                self.line.mark_set(INSERT,'1.0')
                self.line.see(INSERT)
        return 'break'

if __name__ == '__main__':
    Notepad()
Quote
#2
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: http://www.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf
see the text write-up here: http://infohost.nmt.edu/tcc/help/pubs/tk.../text.html
especially the part about selecting text that begins here: http://infohost.nmt.edu/tcc/help/pubs/tk...index.html

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: https://search.packtpub.com/?query=tkint...=Available 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)
Quote

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 380 Jun-06-2019, 04:51 AM
Last Post: klllmmm

Forum Jump:


Users browsing this thread: 1 Guest(s)