The main aim here is too bind events to entry fields which are created dynamically using loops and then fetch the values from those fields, how ever i am having issues here to create a function that grabs the text from the entry field as soon as the user starts typing in the box.
from tkinter import Tk, LEFT, BOTH, StringVar
from tkinter.ttk import Entry, Frame
import functools
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Entry")
self.pack(fill=BOTH, expand=1)
self.contents = []
self.ent = []
for i in range(0,5):
self.contents.append(StringVar())
# give the StringVar a default value
for i in range(0,5):
self.entry = Entry(self)
self.entry.grid(row=0,column=i)
self.entry["textvariable"] = self.contents[i]
self.entry.bind('<KeyRelease>', self.on_changed)
self.ent.append(self.entry)
def on_changed(self, event):
print('contents: {}'.format(self.contents.get()))
return True
def main():
root = Tk()
ex = Example(root)
root.geometry("800x400")
root.mainloop()
if __name__ == '__main__':
main()
When you use bind
, the event object that is passed to the callback includes a reference to the widget, which you can use to get the value of the entry.
Example
def on_changed(self, event):
entry = event.widget
print('contents: {}'.format(entry.get()))
return True
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.