我正在尝试创建一个函数,将函数绑定到#34;插入'中的下一个输入字段。帧。除了在最后一个输入字段的情况下,它将在所有字段中返回输入的文本。我尝试使用迭代器来获取下一个输入字段无济于事。我是Python和Tkinter的新手。我的主要问题围绕着" getinsert"功能,我如何专注于下一个输入字段?有没有办法获得当前的焦点并添加1?另外,我如何检查它是否在最后一个字段上,以便我可以调用" getinsert"功能
非常感谢任何帮助
import tkinter as tk
from tkinter import messagebox
master = tk.Tk()
insertnames = ["Title", "Location", "Tags", "Section", "Blah"]
insertentries = {}
insertlabels = {}
main = tk.Frame(master)
insert = tk.Frame(master)
currentnames = iter(insertnames[0])
def mainmenu():
main.pack()
insertbutton.pack(fill = 'x')
def insertcall():
main.forget()
insert.pack()
insertform()
insertentries["Title"].bind('<Return>', insertfocus)
insertentries["Location"].bind('<Return>', insertfocus)
insertentries["Tags"].bind('<Return>', getinsert)
def getinsert(event):
for entry in insertentries:
print (insertentries[entry].get())
insertentries[entry].delete(0, 'end')
insert.forget()
mainmenu()
def insertfocus(event):
currentnames.next()
insertentries[currentnames].focus()
def insertform():
i = 0
for name in insertnames:
e = tk.Entry(insert)
e.grid(row = i, column = 1)
insertentries[name] = e
lab = tk.Label(insert, text = name)
lab.grid(row = i, column = 0)
insertlabels[name] = lab
i += 1
insertbutton = tk.Button(main, text = "Insert", command = insertcall)
mainmenu()
tk.mainloop()
答案 0 :(得分:0)
我知道这不是最好的选择,但是冗长的方法就是硬编码。对词典有更多经验的人可能能够在循环中做到这一点,但我不能:)
import tkinter as tk
insertnames = ["Title", "Location", "Tags", "Section", "Blah"]
obj = []
root = tk.Tk()
firstFrame = tk.Frame(root)
entryFrame = tk.Frame(root)
def mainmenu():
firstFrame.pack()
insertButton.pack(fill='x')
def insertcall():
firstFrame.forget()
entryFrame.pack()
title = tk.Entry(entryFrame)
title.bind('<Return>', lambda event: focus(title))
title.grid(row=0, column=1)
obj.append(title)
tk.Label(entryFrame, text='Title').grid(row=0, column=0)
location = tk.Entry(entryFrame)
location.bind('<Return>', lambda event: focus(location))
location.grid(row=1, column=1)
obj.append(location)
tk.Label(entryFrame, text='Location').grid(row=1, column=0)
tags = tk.Entry(entryFrame)
tags.bind('<Return>', lambda event: focus(tags))
tags.grid(row=2, column=1)
obj.append(tags)
tk.Label(entryFrame, text='Tags').grid(row=2, column=0)
section = tk.Entry(entryFrame)
section.bind('<Return>', lambda event: focus(section))
section.grid(row=3, column=1)
obj.append(section)
tk.Label(entryFrame, text='Section').grid(row=3, column=0)
blah = tk.Entry(entryFrame)
blah.bind('<Return>', lambda event: focus(blah))
blah.grid(row=4, column=1)
obj.append(blah)
tk.Label(entryFrame, text='Blah').grid(row=4, column=0)
def focus(entryobj):
i = 0
for item in obj: #for each try and get next item unless it gives an error (then it must be the last entry so put it to 0)
try:
if item == entryobj:
obj[i+1].focus_set()
break
except IndexError:
obj[0].focus_set()
i += 1
insertButton = tk.Button(firstFrame, text='Insert', command=insertcall)
insertButton.pack()
mainmenu()
root.mainloop()
答案 1 :(得分:0)
您正在复制Tab键的功能,以便在一组小部件之间移动。这使用虚拟事件<<NextWindow>>
和<<PrevWindow>>
。所以要做的是让你的<Return>
处理程序生成此事件。然后,对于最后一个条目,您要转到特定小部件。我已经创建了一个这样做的例子。你可以使用这样的东西和一组小部件,或者你可以安排检查包含框架中的小部件,如果你深入研究tk_focusNext函数正在做什么来计算下一个要去的小部件。
编辑:通过从python生成遍历事件来模拟tk::TabToWindow
函数
import tkinter as tk
class MyEntry(tk.Entry):
"""Entry widget with Return bound to provide tabbing functionality"""
def __init__(self, master, **kw):
tk.Entry.__init__(self, master, **kw)
self.next_widget = None
self.bind("<Return>", self.on_ret)
def on_ret(self, ev):
if self.next_widget:
self.event_generate('<<TraverseOut>>')
self.next_widget.focus()
self.next_widget.event_generate('<<TraverseIn>>')
else:
self.event_generate('<<NextWindow>>')
return "break"
def set_next(self, widget):
"""Override the default next widget for this instance"""
self.next_widget = widget
def add_entry(parent, row, **kwargs):
widget = MyEntry(parent, **kwargs)
widget.grid(row=row, column=0, sticky=tk.NSEW)
return widget
def main(args=None):
root = tk.Tk()
frame = tk.LabelFrame(root, text="Entries", width=200, height=200)
entries = []
for row in range(4):
e = add_entry(frame, row)
entries.append(e)
e.set_next(entries[0])
frame.grid_columnconfigure(0, weight=1)
frame.grid_rowconfigure(0, weight=0)
button = tk.Button(root, text="Exit", command=root.destroy)
frame.grid(row=0, column=0, sticky=tk.NSEW)
button.grid(row=1, column=0, sticky=tk.SE)
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
entries[0].focus_set()
root.mainloop()
if __name__ == '__main__':
main()
答案 2 :(得分:0)
您可以将有问题的Entry小部件的
def entry_next(self, event):
event.widget.tk_focusNext().focus()
return("break")