JamesLC
JamesLC

Reputation: 55

Python console output to variable

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

Upvotes: 2

Views: 1193

Answers (3)

Jorgen
Jorgen

Reputation: 195

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.

Upvotes: 1

R4PH43L
R4PH43L

Reputation: 2200

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.

Upvotes: 1

Stevo Mitric
Stevo Mitric

Reputation: 1619

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.

Upvotes: 2

Related Questions