iVvaibhav iVvaibhav - 1 year ago 62
Python Question

How can I get the value of 'username' inside the function Login() to use it on another python program?

from Tkinter import *

root = Tk()

L1 = Label(root, text="Login page", bg = "blue", fg = "white")
L1.pack(fill = X, ipadx = 5, ipady = 5)

V = StringVar(root, value='Enter username here')
E1 = Entry(root, textvariable=V)
E1.pack(side = LEFT, padx = 5, pady = 5)

def Login():
username = V.get()
print "Username is '" + username + "'"

B1 = Button(root, text ="Login" , command = Login)
B1.pack(side = RIGHT, fill = X, pady=5)


I have been trying to get the value of 'username' in the function Login() to use it on another python program.

I have tried setting it as global variable and changing its scope but I am not getting anything.

I need to use the value of 'Username' outside the function Login(). Please provide your insights.

Answer Source

Think about scope for a moment. When your program ends, all memory (meaning variables, objects, etc.) are released. The only 2 ways I can think of to pass something from one program to another is:

1) Write the username value to a file which the next program can read as part of its startup.

2) Have a third "controller" or "launcher" program that runs the program above, takes a return value from that program, then passes that value as a parameter to the next program.

But in any case, you will have to save that value past the scope of the program above.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download