2012-12-22 13 views
7

Ho un widget Ttk Notebook contenente 8 frame, quindi 8 schede. Ogni frame contiene un widget di testo. Ho un pulsante al di fuori del widget Blocco note e desidero inserire il testo nelle schede correnti Widget testo quando viene premuto questo pulsante.Trovare la scheda attualmente selezionata di Ttk Notebook

Questo sembrerebbe richiedere l'elaborazione di quale widget nel Notebook è attualmente selezionato, ma non riesco a trovare come farlo. Come trovo la scheda attualmente selezionata?

In alternativa, come posso implementare ciò che voglio?

Se aiuta, ecco il codice per il mio taccuino:

self.nb = Notebook(master) 
self.nb.pack(fill='both', expand='yes', padx=10, pady=10) 
self.frames = [] 
self.texts = [] 
for i in xrange(8): 
    self.frames.append(Frame()) 
    self.nb.add(self.frames[i]) 
    self.texts.append(Text(self.frames[i])) 
    self.texts[i].pack(fill='both') 

risposta

13

È possibile recuperare la scheda selezionata attraverso select metodo. Tuttavia, questo metodo restituisce un tab_id che non è molto utile così com'è. index convertirlo al numero della scheda selezionata.

>>> nb.select() 
'.4299842480.4300630784' 
>>> nb.index(nb.select()) 
2 

Nota che si coud anche ottenere ulteriori informazioni sulla scheda selezionata utilizzando tab

>>> nb.tab(nb.select(), "text") 
'mytab2' 

Si potrebbe guardare Notebook documentazione di riferimento: http://docs.python.org/3/library/tkinter.ttk.html#notebook scheda

+0

Ahhh, esattamente quello che volevo! Avevo difficoltà a setacciare la documentazione, quindi devo aver perso questo. Ma sì, questo è perfetto. – Matthew

1

Io non sono un esperto di tutti, ma spero che possa aiutare con alcuni "occhi nuovi". immagino che potrebbe essere qualcosa che coinvolge

def buttonclick(): 
     somevariablename = focus_get() 
     #Print your text into the somevariable notebook could be 
     #something like(not sure about the syntax): 
     focusednotebook = somevariablename 
     focusednotebook.insert('1.0', 'your text here') 

yourbutton = Button(parent, text = "button name", command = buttonclick) 
yourbutton.pack() 

Speranza funziona o si ottiene nella giusta direzione.

Non esitate a modificare come io sono abbastanza nuovo qui amd con Python :-)

+0

Ahhh, sì, questo è molto simile a quello che ho finito per usare. Non proprio quello che sto cercando, ma fa quello che mi serve. – Matthew

0

Ci sono due modi semplici per vedere scheda che viene selezionato:

nb.select() # returns the Tab NAME (string) of the current selection 

e

nb.index('current') # returns the Tab INDEX (number) of the current selection 

Procedimento .select() può anche essere utilizzato per selezionare quale scheda è attualmente attiva, tramite nb.select(tabId) . Senza l'arg, restituisce il tabId (nella forma "nome") della selezione corrente.

Il .index(tabId) converte un tabId in un indice numerico. Può anche prendere la stringa "fine" che restituirà il numero di schede. Quindi, nb.index(tkinter.END) è come un metodo len() per un widget di un notebook.

Quando non ci sono schede, .select() restituisce una stringa vuota, ma .index('current') genera un'eccezione.Quindi, se vuoi l'indice, direi

if nb.select(): 
    idx = nb.index('current') 

è il modo migliore per andare.

Nel tuo caso particolare, vorrai probabilmente prendere il nome attuale della scheda del taccuino e quindi convertire quel nome nel widget di testo figlio attuale, tramite il metodo nametowidget(), per la manipolazione. Quindi ...

tabName = notebook.select() 
if tabName: 
    textWidget = notebook.nametowidget(tabName) # here, 'notebook' could be any widget 
    textWidget.insert(pos, text, tags) 

Il metodo nametowidget(name) associa un nome al widget Tkinter reale. È un metodo richiamabile da qualsiasi widget reale.

Problemi correlati