Python Forum
[Tkinter] Select All text without space on the righthand side
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()
Reply


Messages In This Thread
Select All text without space on the righthand side - by Vicolas - Feb-28-2019, 06:17 PM

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

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020