Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter - Setting the Current Selection in TextWidget

Tags:

python

tkinter

I am looking for a way to set the current selection inside a Tkinter text widget.

I already tried to set it with mark_set(), but it doesn't work...

text_widget.mark_set("sel.first", 1.0)
text_widget.mark_set("sel.last",  END)

Would anyone know how to work this one out?

like image 439
linaa Avatar asked Aug 31 '11 11:08

linaa


1 Answers

sel is a tag, not a mark, so you need to use the tag commands, such as tag_add

import Tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.text = tk.Text(self)
        self.text.pack()
        self.text.insert("1.0", "Hello, world")
        self.text.tag_add("sel", "1.7", "1.12")
        self.text.focus_force()

app = SampleApp()
app.mainloop()
like image 199
Bryan Oakley Avatar answered Oct 19 '22 03:10

Bryan Oakley