Python console output to variable
Question: 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


Answer: 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.

