2

I have seen a few posts on this site and some others that cover a similar topic don't quite seem to reach the result i am looking for with python v3. My aim is to have a popup window containing two entry boxes for a username and a password which i can then output as variables named username and password, to then in turn use to login to a website which i already have scripted. The code i have so far is:

from tkinter import *

def show_entry_fields():
   print("Username: %s\nPassword: %s" % (e1.get(), e2.get()))

master = Tk()
Label(master, text="Username").grid(row=0)
Label(master, text="Password").grid(row=1)

e1 = Entry(master)
e2 = Entry(master)

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)

Button(master, text='Quit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Submit', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)

I am getting stuck with working out how to take the output that shows in the console after pressing submit and getting these two lines to become the variables? Any help or suggestions would be greatly appreciated. Thanks in advance,

James

3 Answers 3

2

You can use tkinter's variables:

username = StringVar()
password = StringVar()

And then when you define the entrys, add argument textvariable:

e1 = Entry(master, textvariable = username)
e2 = Entry(master, textvariable = password)

To get the value from this variable, call .get() function on it.

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

Comments

1

You also need to use lambda to send the arguments to the subroutine. Try this:

from tkinter import *

def show_entry_fields(v1, v2):
   print("Username: %s\nPassword: %s" % (v1.get(), v2.get()))


if __name__ == "__main__":
  master = Tk()
  Label(master, text="Username").grid(row=0)
  Label(master, text="Password").grid(row=1)

  v1 = StringVar()
  v2 = StringVar()

  e1 = Entry(master, textvariable=v1)
  e2 = Entry(master, textvariable=v2)

  e1.grid(row=0, column=1)
  e2.grid(row=1, column=1)

  Button(master, text='Quit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
  Button(master, text='Submit', command=(lambda : show_entry_fields(v1, v2))).grid(row=3, column=1, sticky=W, pady=4)

  master.mainloop()

UPDATE: I guess I should do as I preach, so I added a local context to the main part.

3 Comments

Nope, OP doesn't need to send any arguments,
OK, not when v1 and v2 are global variables, but when he makes it into a real program callling it from a local context, eg either a subroutine or 'if name == "main":', it is necessary. He will get in trouble later.
Thank you to all who responded. I ended up using the second block of code to solve the problem. I changed the variable names and that seemed to work. Thanks, James
1

I suppose your question is "How do I get access on a self destructing window?" - You don't.

First Approach

You access it before it destroys itself.

How is that? - You bind and event from the child (pop-up) to the parent and as soon as your submit button is fired, you generate the event as a notification for the parent like "I am soon to be destroyed" (see this question answered by Bryan Oakley for more info).

One very important thing - the window must not destruct itself as long as it should return something. The parent kills it after retrieving the data from it.

Second Approach

You use e.g. tkinter.StringVar for storing your data.

But you do not have them inside your popup but inside your parent widget, so they survive even the Entry widgets are destroyed.

For this approach, use the Information from Stevo but pass them on to the pop up during construction time as parameters stored inside your parent.

Comments

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.