1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| from Tkinter import *
class MyText(Text):
def __init__(self,parent,textvariable=None,**kwargs):
Text.__init__(self,parent,**kwargs)
self.bind("<KeyRelease>",self.onKey)
self.var=textvariable
if self.var:
self.var.trace("w",self.onTrace)
def insert(self,*args):
Text.insert(self,*args)
self.onKey()
def delete(self,*args):
Text.delete(self,*args)
self.onKey()
def onKey(self,event=None):
if self.var:
self.var.set(self.get(1.0,END)[:-1])
def onTrace(self,*args):
if self.var:
self.value=self.var.get()
self.delete(1.0,END)
self.insert(1.0,self.value)
if __name__=='__main__':
root=Tk()
var=StringVar()
t=MyText(root,width=20,textvariable=var,font=('Arial',12,'normal'))
t.pack()
e=Entry(root,textvariable=var)
e.pack()
var.set('textvariable')
root.mainloop() |
Partager