Я наконец нашел способ получить именно то поведение, которое хочу:
from Tkinter import Text, BOTH
import re
class T(Text):
def __init__(self, *a, **b):
# Create self as a Text.
Text.__init__(self, *a, **b)
#self.bind("<Button-1>", self.click)
self.bind("<Key>", self.key)
self.bind("<Control-v>", self.paste)
def key(self,k):
if k.char and k.char not in "atgcATGC":
return "break"
def paste(self,event):
clip=self.selection_get(selection='CLIPBOARD')
clip=clip.replace("\n","").replace("\r","")
m=re.match("[atgcATGC]*",clip)
if m and m.group()==clip:
self.clipboard_clear()
self.clipboard_append(clip)
else:
self.clipboard_clear()
return
t = T()
t.pack(expand=1, fill=BOTH)
t.mainloop()