{Possible_Spam} tkinter text editor

John McMonagle jmcmonagle at velseis.com.au
Thu Mar 8 19:29:49 EST 2007


Gigs_ wrote:
> I'm writing text editor.
> 
> How to enable/disable (cut, copy etc.) when text is selected/not selected


Bind the Button1-ButtonRelease event to a function which checks the 
length of the SEL tag of the text widget.  If it is zero length, disable 
the appropriate menu entries, if it is non-zero, enable the appropriate 
menu entries.

Simple example:


from Tkinter import *
root = Tk()

textWidget = Text(root)
textWidget.pack()

def onButton1Release(event):
     if len(textWidget.tag_ranges(SEL)) == 0:
         print 'No text selected.  Disable appropriate menu entries'
     else:
         print 'Some text selected.  Enable appropriate menu entries'

textWidget.bind('<Button1-ButtonRelease>', onButton1Release)

root.mainloop()



More information about the Python-list mailing list