2010-11-01 20 views

risposta

7

Per un widget Tkinter.Text o Tkinter.Entry, è possibile utilizzare:

content = text.selection_get() 

esempio interattivo di ottenere il testo selezionato sia da una voce e da un widget di testo nella console Python:

>>> import Tkinter 
>>> w = Tkinter.Tk() 
>>> e = Tkinter.Entry(w) 
>>> e.pack() 
>>> t = Tkinter.Text(w) 
>>> t.pack() 
#(scribble at the widgets in the created window, and select text in the Entry) 
>>> e.selection_get() 
'1234' 
#(select text) 
>>> t.selection_get() 
'1234' 
>>> 

Secondo a commento di Bryan Oakley:

selection_get è un metodo widget generico disponibile per tutti i widget. È non sempre ottenere la stringa selezionata da un widget di testo. Potrebbe, ma non è garantito. Quello che fa è ottenere la cosiddetta selezione "X" (dalle radici X11 di tk). Se si imposta exportselection su False per il widget Testo, selection_get non funzionerà. Quindi il tuo suggerimento funzionerà nel caso normale, ma non in tutti i casi.

+0

Il widget Testo non ha un metodo 'selection_get'. –

+0

@BryanOakley: quale versione stai usando? Sto usando Tkinter fornito con Python 2.7, ei miei widget di testo hanno sicuramente un 'selection_get'. (Ho appena provato di nuovo in una sezione della console solo per essere sicuro e ha funzionato). – jsbueno

+1

Avrei dovuto essere più preciso: il widget di testo non ha un metodo 'selection_get'. 'selection_get' è un metodo widget generico disponibile per tutti i widget. Non _non_ ottiene la stringa selezionata da un widget di testo. Potrebbe, ma non è garantito. Quello che fa è ottenere la cosiddetta selezione "X" (dalle radici X11 di tk). Se si imposta 'exportselection' su False per il widget di testo,' selection_get' non funzionerà. Quindi il tuo suggerimento funzionerà nel caso normale, ma non in tutti i casi. –

6

utilizzare il metodo get. Se vuoi che il testo selezionato usi gli indici SEL_FIRST e SEL_LAST.

[modifica]] uno dei commenti ha speculato che questo non ha funzionato con Tkinter perché non c'era nessun attributo "sel". "sel" non è un attributo, è un tag. I tag sono una funzionalità straordinariamente potente del widget di testo.

Ecco un esempio:

import Tkinter as tk 
import tkFont 

class App: 
    def __init__(self): 
     root=tk.Tk() 
     self.text = tk.Text(root) 
     self.text.pack() 
     self.button = tk.Button(root, text="Get Selection", command=self.OnButton) 
     self.button.pack() 
     root.mainloop() 

    def OnButton(self): 
     print "selected text: '%s'" % self.text.get(tk.SEL_FIRST, tk.SEL_LAST) 


app=App() 

Per questa demo al lavoro, digitare il testo nella casella di testo e premere il pulsante.

+0

non vedo come attributo "SEL" nel testo o widget Entry - forse ti riferisci a metodi disponibili da TCL, ma che sono racchiusi con un altro nome in Python? – jsbueno

+0

@jsbueno: 'sel' non è un attributo. È un tag di testo che rappresenta la selezione ed è disponibile sotto Tkinter. –

+0

Non è necessario utilizzare la stringa sottostante definita da Tk. 'Tkinter' ha una variabile definita per accedervi -' SEL_FIRST' e 'SEL_LAST'. – ArtOfWarfare

4

Il widget di testo ha un tag speciale chiamato 'sel', accessibile tramite Tkinter.SEL e verificabile tramite text_widget.tag_ranges (Tkinter.SEL), che consente di recuperare il testo "selezionato". Ecco un semplice esempio:

if textWidget.tag_ranges(Tkinter.SEL): 
    print('SELECTED Text is %r' % textWidget.get(Tkinter.SEL_FIRST, Tkinter.SEL_LAST)) 
else: 
    print('NO Selected Text') 

Se si desidera una soluzione leggermente più avanzato, si può anche provare:

ranges = textWidget.tag_ranges(Tkinter.SEL) 
if ranges: 
    print('SELECTED Text is %r' % textWidget.get(*ranges)) 
else: 
    print('NO Selected Text') 
+1

Plus1 per verificare se è presente un testo selezionato per iniziare. Altrimenti SEL_FIRST/LAST errore :-) –

Problemi correlati