我想用Tkinter创建一个条目,用户可以在其中键入其电话号码,并且文本会动态地改变,这样一旦完成它就会变成+34 1234567890。在
在我的代码中,用于设置光标位置的函数.icursor(n)起初不能正常工作,但后来,超过了前缀,它就正常工作了。在
这是我的代码片段(它属于一个大得多的代码片段)。在from Tkinter import *
def TelephoneCheck(self,Vari):
Plain = Vari.get()
Plain = list(Plain)
Plain_flat = []
for element in Plain:
try:
check = int(element)
Plain_flat.append(element)
except: pass
if len(Plain_flat) > 2:
Plain_flat.insert(2,' ')
Plain = ''.join(Plain_flat)
Plain = '+'+Plain
self.istn.set(Plain)
self.InsertTelephoneNumber.icursor(len(Plain))
def CreateInsertTelephoneNumber(self,X,Y,color='white'):
self.istn = StringVar()
self.istn.trace('w', lambda name, index, mode, istn=self.istn: self.TelephoneCheck(istn))
self.InsertTelephoneNumber = Entry(Body,textvariable=self.istn)
self.InsertTelephoneNumber.config(bg=color)
self.InsertTelephoneNumber.place(height=20,width=230,y=Y+27,x=X+245)
def LabelBody(self,X,Y):
TelephoneText = Label(Body,text='Telephone Number *')
TelephoneText.place(y=Y+4,x=X+243)
self.CreateInsertTelephoneNumber(X,Y)
如您所见,理论上,每次用户添加数字时,位置都应该设置在字符串的末尾。
{{cd3}不象前面的cd3}那样理解。在
如果需要更多代码,我会更新帖子。在
谢谢你的时间和帮助!在