How can I create a row of single-character text boxes and have the cursor automatically advance as the user types?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật