Python Tkinter how to take the input from multiple Entry widgets stored in one variable
Question: my code is the following.

    
    
    from tkinter import *
    def TakeInput():
        print(tb.get()) #This will print Entry1 input
        print(tb.get()) #This will print Entry2 input
    tk=Tk()
    #Entry 1
    tb=Entry(tk) #Both Entry1 and Entry2 are stored in the same variable: tb
    tb.pack()
    #Entry 2
    tb=Entry(tk) #Both Entry1 and Entry2 are stored in the same variable: tb
    tb.pack()
    #Button
    b=Button(tk,text="PrintInput",command= TakeInput)
    b.pack()
    tk.mainloop()
    

All I am trying to do is to display both entry 1 and entry 2 input when both
are assigned to the same variable.

Note that I am a Python newbie.


Answer: If you want to do it automatically, you have to control strings in entry
widgets when they modified. You can do it with `StringVar`. You dont need a
button, when the entry1's text equals to entry2's text, it will automatically
prints.

    
    
    from tkinter import *
    
    def TakeInput():
        print(tb1.get())
        print(tb2.get())
    
    def on_entry1_changed(*args):
        if sv_entry1.get() == sv_entry2.get():
            TakeInput()
    
    def on_entry2_changed(*args):
        if sv_entry1.get() == sv_entry2.get():
            TakeInput()
    tk=Tk()
    
    #Entry 1
    sv_entry1 = StringVar()
    sv_entry1.set("Entry1 text")
    sv_entry1.trace("w", on_entry1_changed)
    
    tb1=Entry(tk, textvariable=sv_entry1)
    tb1.pack()
    
    #Entry 2
    sv_entry2 = StringVar()
    sv_entry2.set("Entry2 text")
    sv_entry2.trace("w", on_entry2_changed)
    
    tb2=Entry(tk, textvariable=sv_entry2)
    tb2.pack()
    
    tk.mainloop()
    

If you want to do it with pressing button, you have to modify TakeInput
function like this:

    
    
    from tkinter import *
    def TakeInput():
        if tb1.get() == tb2.get():
            print tb1.get()
    tk=Tk()
    
    #Entry 1
    tb1=Entry(tk)
    tb1.pack()
    
    #Entry 2
    tb2=Entry(tk)
    tb2.pack()
    
    #Button
    b=Button(tk,text="PrintInput",command= TakeInput)
    b.pack()
    tk.mainloop()
    

