'enter special character in between text in python at after certain length
code is running good in main project , took some sample code from that as below
from tkinter import *
root = Tk()
root.geometry('200x200')
root.title('Testing')
num1 = StringVar()
num2 = StringVar()
def callback(integer):
if integer.isdigit():
return True
elif integer == "":
return True
else:
return False
def limit_no1(no):
c = no.get()[0:16]
no.set(c)
num1.trace("w", lambda name, index, mode, sv=num1: limit_no1(sv))
reg = root.register(callback)
ent1 = Entry(root, textvariable=num1)
ent1.pack()
ent1.config(validate="key", validatecommand=(reg, '%P'))
this has some condition as accepting only numbers and up 16 digits but as like some web apps i need spacing after 4 characters is there any method for that so that i can apply to this and and for input of currency also as ',' in btw data
I worked on some code but indexing getting issue in indexing
the below code continuation of above only
num2.trace("w", lambda name, index, mode, sv=num2: limit_no2(sv))
ent2 = Entry(root, textvariable=num2)
ent2.pack(pady=20)
ent2.config(validate="key", validatecommand=(reg, '%P'))
def limit_no2(no):
c = no.get()[0:19]
split_string = [c[i:i + 4] for i in range(0, len(c), 4)]
if len(split_string) > 1:
final_string = ''
for i in range(len(split_string)):
final_string += split_string[i]
final_string += " "
no.set(final_string)
else:
no.set(c)
root.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
