Home > Software engineering >  how to update getting results from an entry box automatically?
how to update getting results from an entry box automatically?

Time:04-04

I want to update getting results from an entry box in a way that when an integer enters, the equivalent rows of entry boxes appear below that. I have written the below code to make it work using a button. However, I want to make it happen automatically without a button as I entered the number, the rows update. I checked one way of doing that is using the after(). I placed after after() in the function and out of the function but it is not working.

from tkinter import *

root = Tk()
root.geometry("400x400")

n_para = IntVar()

label1 = Label(root, text="Numeric parameters")
label1.grid(row=0, column=0)

entry1 = Entry(root, textvariable=n_para)
entry1.grid(row=0, column=1)

def update():
    for i in range(1, n_para.get() 1):
        entryX = Entry(root)
        entryX.grid(row=i 1, column=0)

        entryY = Entry(root)
        entryY.grid(row=i 1, column=1)

        entryZ = Entry(root)
        entryZ.grid(row=i 1, column=2)

        root.after(100, update)

root.after(1, update)

button1 = Button(root, text="update", command=update)
button1.grid(row=1, column=0)

root.mainloop()

CodePudding user response:

You should try using the <KeyRelease> event bind.

import tkinter as tk

def on_focus_out(event):
    label.configure(text=inputtxt.get())


root = tk.Tk()
label = tk.Label(root)
label.pack()

inputtxt = tk.Entry()
  
inputtxt.pack()

root.bind("<KeyRelease>", on_focus_out)

root.mainloop()

This types the text entered in real-time.

Edited Code with OP's requirement:

from tkinter import *

root = Tk()
root.geometry("400x400")

n_para = IntVar()

label1 = Label(root, text="Numeric parameters")
label1.grid(row=0, column=0)

entry1 = Entry(root, textvariable=n_para)
entry1.grid(row=0, column=1)

def upd(event):
    x = entry1.get()
    if not x.isnumeric():
        x = 0
    for i in range(1, int(x) 1):
        entryX = Entry(root)
        entryX.grid(row=i 1, column=0)

        entryY = Entry(root)
        entryY.grid(row=i 1, column=1)

        entryZ = Entry(root)
        entryZ.grid(row=i 1, column=2)

#        root.after(100, update)

root.bind("<KeyRelease>", upd)

# button1 = Button(root, text="update", command=update)
# button1.grid(row=1, column=0)

root.mainloop()
  • Related