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
  [Tkinter] Adding space between Notebook tabs Columbo 4 274 Jul-10-2019, 10:46 PM
Last Post: Columbo
  Update value selected in option menu when select an item from text box klllmmm 2 288 Jun-06-2019, 04:51 AM
Last Post: klllmmm
  [Tkinter] Unable to create checkbox and select at run time tej7gandhi 5 373 May-05-2019, 04:57 PM
Last Post: tej7gandhi
  [WxPython] Any dialog that allow user to select file OR folder? buran 3 330 Apr-03-2019, 06:33 PM
Last Post: Yoriz
  [WxPython] How to show remove button at the right side of the hovering item of a combobox popup? indrajitmajumdar 0 242 Mar-28-2019, 11:24 AM
Last Post: indrajitmajumdar
  populate list with images and be able to select them ricardons 0 277 Jan-11-2019, 03:45 PM
Last Post: ricardons
  [PyQt] Layout unwanted space WatcherMagic 2 412 Dec-12-2018, 09:37 PM
Last Post: WatcherMagic
  [Tkinter] Two text files in Text widget (python 3) atlass218 21 2,033 Nov-09-2018, 06:24 PM
Last Post: atlass218
  Cannot get tree.select to work for the life of me. WuchaDoin 0 433 Oct-16-2018, 06:27 PM
Last Post: WuchaDoin
  [Tkinter] How to show and hide tkinter entry box when select yes from drop down Prince_Bhatia 1 1,295 Jun-12-2018, 08:05 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)