Seite 1 von 1

anfangs- und endindex von markiertem text

Verfasst: Freitag 3. März 2006, 13:24
von jAN
Ich brauch mal wieder eure Hilfe!
Kann mir jemand sagen, wie ich den Anfangsindex und den Endindex von markiertem Text in einem Text()-Feld bekommen kann?
ich dachte da an

Code: Alles auswählen

TextBox.selection_own()
index1=TextBox.index(INSERT)
index2=index1+/-len(text)
(so in der art)
aber ich weis dadurch ja nciht ob der curser am anfang oder am ende des makierten textes steht...

Verfasst: Mittwoch 8. März 2006, 20:45
von jAN
keiner ne ahnung?

Verfasst: Mittwoch 8. März 2006, 23:21
von Mr_Snede
Meine Suche mit google ergab:
Tkinter provides a number of different index types:
...
selection (SEL_FIRST, SEL_LAST)
...
--> http://effbot.org/tkinterbook/text.htm

und
SEL_FIRST
If some of the text in the widget is currently selection (as by dragging the mouse over it), this is the position before the start of the selection. If you try to use this index and nothing is selected, a TclError exception will be raised.

SEL_LAST
The position after the end of the selection, if any. As with SEL_FIRST, you'll get a TclError exception if you use such an index and there is no selection.
--> http://infohost.nmt.edu/tcc/help/pubs/t ... index.html

Schau mal, ob es dir schon weiterhilft.

cu Sebastian

Verfasst: Donnerstag 9. März 2006, 07:24
von jAN
Danke Mr_Snede!
funktioniert prima...
man muss nur nacheinander abfragen: erst den anfangsindex und dann den endindex...

Verfasst: Donnerstag 9. März 2006, 11:04
von Mr_Snede
Magst du ein lauffähiges Beispiel unter "Codesnippets" stellen?

Ich meine was klitze kleines:
Textwidget mit etwas Text und einem Button, der bei Betätigung die Positionen auf der Konsole ausgibt.

cu Sebastian

Verfasst: Donnerstag 9. März 2006, 17:00
von jAN
meine idee war eigentlich ein kleiner editor, mit dem man html-tags am anfang und am ende des markierten textes setzen kann... so ich mach mal ein kleines beispiel und stell es zu den codesnippets...

Verfasst: Freitag 10. März 2006, 00:08
von BlackJack
So sähe ein einfaches Beispiel aus:

Code: Alles auswählen

import Tkinter as Tk

def selection_printer(text_widged):
    def print_selection():
        try:
            print map(text_widged.index, (Tk.SEL_FIRST, Tk.SEL_LAST))
        except Tk.TclError:
            print 'no selection'
    return print_selection

root = Tk.Tk()

text = Tk.Text(root)
text.insert(Tk.INSERT, 'The knights who say Ni!')
text.focus()
text.pack()

button = Tk.Button(root, text='Test', command=selection_printer(text))
button.pack()

root.mainloop()

Verfasst: Freitag 10. März 2006, 13:17
von jAN
ich hab ein etwas großerse beispiel unter codesnippets reingestellt...