开发者

How to get the last value of a list of tkinter Entry widgets

开发者 https://www.devze.com 2023-01-12 06:26 出处:网络
I have a list of n Entry widgets. Every widget accepts only one character, and then the focus is passed to the next one. I would like to \".get()\" the values of the n widgets, but I can\'t get the la

I have a list of n Entry widgets. Every widget accepts only one character, and then the focus is passed to the next one. I would like to ".get()" the values of the n widgets, but I can't get the last one. Here is the sample code:

import Tkinter as tk

def vf(event):
    actual=entrylist.index(root.focus_get())
    print "--",len(entrylist),actual

    if event.char.upper() in ('V', 'F', ' '):
        print event.char
        if actual<len(entrylist)-1:
            entrylist[actual+1].focus_set()
        else:
            #set focus to another widget?
            for x in entrylist:
                print "-",x.get(),"-"
                #the last character is lost!

    elif event.keysym not in ('Alt_r', 'Alt_L', 'F4'):
        print event.keysym
        return 'break'


root= tk.Tk()
entrylist=[]
for i in xrange(4):
    e=tk.Entry(width=1)
    e.grid()
    e开发者_如何学运维.bind("<KeyPress>",vf)
    entrylist+=[e]

root.mainloop()


The value you enter into the fourth Entry widget is only stored after the event has finished. That is, after the call to vf(event) is complete.

I have added a button which displays the contents of the Entry widgets when pressed. This will display the contents of all four widgets.

Otherwise access the contents of the Entry widgets after the call to vk is complete.

import Tkinter as tk

def vf(event):
    actual = entrylist.index(root.focus_get())
    print "--", len(entrylist), actual

    if event.char.upper() in ('V', 'F', ' '):
        print event.char
        if actual < len(entrylist) - 1:
            entrylist[actual + 1].focus_set()
        else:
            #set focus to another widget?
            for x in entrylist:
                print "-", x.get(), "-"
                #the last character is lost!

def show():
    for x in entrylist:
        print '-', x.get(), '-'

root = tk.Tk()
entrylist = []
for i in xrange(4):
    e = tk.Entry(root, width=10)
    e.grid()
    e.bind("<KeyPress>", vf)
    entrylist.append(e)

b = tk.Button(root, text='show', command=show)
b.grid()

root.mainloop()

Edit Answering the question in the comment.

It appears that the text is only stored in the Entry widget once the key has been released. Therefore, you can check for KeyRelease events in the fourth Entry. Then, at that point, you should be able to access the values stored in all four widgets. The following works (but I don't find it very elegant - there may be a simpler way of doing this).

import Tkinter as tk

def vf(event):
    entry_index = entries.index(root.focus_get())
    if event.char.upper() in ('V', 'F', ' '):
        if entry_index < len(entries) - 1:
            entries[entry_index + 1].focus_set()
    elif event.keysym not in ('Alt_r', 'Alt_L', 'F4'):
        print event.keysym
        return 'break'

def show(event):
    if entries[-1].get():
        # only print the values if the last Entry contains text
        for i, e in enumerate(entries):
            print 'var %s: %s' % (i, e.get())

root = tk.Tk()
entries = []
for i in xrange(4):
    e = tk.Entry(width=10)
    e.grid()
    e.bind("<KeyPress>", vf)
    if i == 3:
        # catch KeyRelease events on the last Entry widget
        e.bind("<KeyRelease>", show)
    entries.append(e)

root.mainloop()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号