I’m making an app for mobile automotive repair for my own personal use. In my customer information section, I have a pane for entering vehicle information. In that vehicle information, I want to include the seventeen digit VIN of the vehicle.
In my label display (a different area of the app), I want the seventeen-digit VIN to be parsed so I can quickly identify the eighth character, and the last eight characters. The eighth is the engine code. The last eight are what the dealerships use when they look up parts.
It seems to me that it would be simpler to concatenate seventeen separate characters and add spaces where I want them than it would be to take a seventeen digit string and parse it.
So, I decided that seventeen individual entry boxes was the right way to go. But, it’s tedious to have to tab after each character to get to the next box. Ergo, I’d like to have it so that once I type a character into the first box, it automatically advances to the second. Once I type a character in the second, it automatically advances to the third. And so on. That way I can just type out the VIN.
I’m not having a problem making the boxes, or getting them to function. But I don’t yet know Python well enough to know what sort of event function I need to create to get the effect I’m looking for.
I am a newbie. As best as you’re able, give me pre-algebra, not calculus, if you catch my drift.
What I’ve tried working with is this:
def advnum1(event):
entry = event.widget
if len(entry.get()) == entry.get_max_length():
next_widget = entry.tkinter_focusNext()
if next_widget:
next_widget.focus()
fnumbox1 = tkinter.Entry(master=cstinfofrmfg, justify="center", bg="#b2b4b7", fg="#000000", width=4)
fnumbox1.place(x=112, y=181.5)
fnumbox1.bind("<KeyRelease>", advnum1)
fnumbox2 = tkinter.Entry(master=cstinfofrmfg, justify="center", bg="#b2b4b7", fg="#000000", width=4)
fnumbox2.place(x=144, y=181.5)
fnumbox3 = tkinter.Entry(master=cstinfofrmfg, justify="center", bg="#b2b4b7", fg="#000000", width=5)
fnumbox3.place(x=176, y=181.5)
But try as I might to change different parts of the expression, everything gives me an error. The get function doesn’t seem to like this arrangement. Other things say there’s no attribute. I tried putting in the variable name of the Entry control. I’ve tried a variety of things. Nothing seems to work. I’m assuming this is just an outline script of sorts. But I’m not sure what should actually be as it is, and what should be substituted with specific variables or widget names.
3
This is how I would implement your idea.
I use the string module for an alphanumeric list.
All Entry
widgets are stored in ‘entry’ list.
Variable ‘q’ determines the total number of Entry
widgets.
I would make sure the User enters text in the correct order.
This is done by disabling all Entry widgets except for the relevant one.
When all data has been entered, collect and display then delete for next input.
Alternatively, collect and store data in dictionary that may have User name as a key.
(I haven’t implemented that… but it’s a logical direction for code expansion)
import tkinter
from string import ascii_letters, digits
entry, result = [], []
alpha = ascii_letters + digits
# place at x, y, width, quantity
x, y, w, q = 10, 20, 20, 16
app = tkinter.Tk()
app.rowconfigure(0, weight = 1)
app.columnconfigure(0, weight = 1)
frame = tkinter.Frame(app, width = w*q+w//2, height = y*3)
frame.grid(row = 0, column = 0, sticky = "nsew")
frame.rowconfigure(0, weight = 1)
frame.columnconfigure(0, weight = 1)
def select(i):
entry[i]["state"] = "normal"
entry[i].delete(0, 'end')
entry[i].focus_set()
def respond(event):
global i
value = entry[i].get()
if value in alpha and len(value) == 1:
result.append(value)
k = i
i = (i+1)%q
if i == 0:
for fun in entry:
fun["state"] = "normal"
fun.delete(0, 'end')
fun["state"] = "disabled"
print(result)
result.clear()
entry[k]["state"] = "disabled"
select(i)
# Create 'q' entry widgets and store in 'entry' list
for a in range(q):
entry.append(tkinter.Entry(
frame, justify="center", bg="#ffffff",
fg="#000000", show = "*", state = "disabled"))
entry[a].place(x=x, y=y, width = w//2)
# Bind every Entry widget
entry[a].bind("<KeyRelease>", respond)
x = x + w
# initialize
i = 0
select(i)
app.mainloop()
2
Okay, I figured it out.
def my_variable(event):
VINbox1.focus()
This is coupled with:
VINbox1 = tkinter.Entry(master=root, bg="#b2b4b7", fg="#000000", width=1, relief=tkinter.FLAT)
VINbox1.bind("<KeyRelease>", my_variable)
No “if” or “len” tests or comparisons needed to shift focus for a single letter or digit. Although, seventeen individual events will have to be written to do as I was inquiring.
A similar version that I found worked for multiple digits, as in a phone number, was:
def advnum1(event):
phtext1 = fnumbox1.get()
phlen1 = len(phtext1)
if phlen1 == 3:
fnumbox2.focus()
def advnum2(event):
phtext2 = fnumbox2.get()
phlen2 = len(phtext2)
if phlen2 == 3:
fnumbox3.focus()
def advnum3(event):
phtext3 = fnumbox3.get()
phlen3 = len(phtext3)
if phlen3 == 4:
altfnumbox1.focus()
This is combined with:
fnumbox1 = tkinter.Entry(master=cstinfofrmfg, justify="center", bg="#b2b4b7", fg="#000000", width=4)
fnumbox1.place(x=112, y=181.5)
fnumbox1.bind("<KeyRelease>", advnum1)
fnumbox2 = tkinter.Entry(master=cstinfofrmfg, justify="center", bg="#b2b4b7", fg="#000000", width=4)
fnumbox2.place(x=144, y=181.5)
fnumbox2.bind("<KeyRelease>", advnum2)
fnumbox3 = tkinter.Entry(master=cstinfofrmfg, justify="center", bg="#b2b4b7", fg="#000000", width=5)
fnumbox3.place(x=176, y=181.5)
fnumbox3.bind("<KeyRelease>", advnum3)
I’m always open to learning new ways to do things. Any suggestions on a simpler code are welcomed.
7