0

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.

1
  • You're edited code wouldn't help. See the edit to my answer. Commented Sep 7, 2014 at 10:47

2 Answers 2

3

Create a list containing the entries and loop through them.

def print_input(*args):
    for entry in entries:
        print(entry.get())

entries = [Entry(tk) for _ in range(2)]
for entry in entries:
    entry.pack()

btn = Button(tk, text="Print", command=print_input)

In your version, you're assigning tb at first to one entry, then to the other. That's not how you store input from multiple widgets in one variable. You're just overwriting the reference to the first widget you have created and stored.

Sign up to request clarification or add additional context in comments.

2 Comments

The two Entry Gadgets should not have the same text when typing on them. I think textvariable shouldn't exist there. What i want is to have all Entry gadgets stored in the variable tb instead of having to assign them to tb,tb2,tb3,tb4,...,tbn and get their inputs separately. I want that because the actual program I am trying to create is making Entry gadgets with loops and gets the input text for each of the Entry Gadgets made by clicking a button.
Than it's even easier: Add them in a list and loop over every element.
1

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()

2 Comments

Thanks for the response but I need the entry gadgets stored in the same variable and print their inputs when the button is pressed.
Why do you need to store entry widgets in the same variable? The second code just does the the job. When they are equal it prints.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.