简体   繁体   中英

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

from Tkinter import *

root = Tk()
root.geometry("230x100")

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)

mainloop()

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.

1) Create a python file say ' global_vars.py ' and add this line in it.

#global_vars.py
global_V = ''

2) Import this global_vars.py wherever you want set the variable as below:

#main.py

from Tkinter import *
import global_vars

root = Tk()
root.geometry("230x100")

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')
#Set the global variable
global_vars.global_V = V
E1 = Entry(root, textvariable=V)
E1.pack(side = LEFT, padx = 5, pady = 5)

3) Consider you want to use this value in python program present in file " test.py ". Just import global_vars.py and use this variable

#test.py

import global_vars.py

def printUserName():
    print "Username is -", global_vars.global_V

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.

If you have to python files, one called main.py that contains your main program (I assumed it was a GUI program) and the login.py file that contains the login program.

main.py

from tkinter import Tk, Label
from login import LoginGUI

class mainGUI(Tk):
    def __init__(self):
        Tk.__init__(self)
        Label(self, text="You need to login first",
              bg="blue", fg="white").pack(fill="x", ipadx=5, ipady=5)
        # open login dialog
        login = LoginGUI(self)
        # wait for the user to log in
        self.wait_window(login)
        username = login.getLogin()
        Label(self,
              text="Your username is " + username).pack(fill="x", ipadx=5, ipady=5)
        self.mainloop()


if __name__ == '__main__':
    mainGUI()

login.py

from tkinter import Toplevel, StringVar, Entry, Button, Label

from tkinter import Toplevel, StringVar, Entry, Button, Label

class LoginGUI(Toplevel):
    def __init__(self, master):
        Toplevel.__init__(self, master)
        self.transient(master)
        self.geometry("230x100")
        Label(self, text="Login page",
              bg="blue", fg="white").pack(fill="x", ipadx=5, ipady=5)
        self.username = ""
        self.usernameVar = StringVar(self, value='Enter username here')
        E1 = Entry(self,
                  textvariable=self.usernameVar)
        E1.pack(side="left", padx=5, pady=5)
        Button(self, text="Login",
               command=self.Login).pack(side="right", fill="x", pady=5)
        E1.focus_set()

    def Login(self):
        self.username = self.usernameVar.get()
        self.destroy()

    def getLogin(self):
        return self.username

If your main program have no GUI, replace Toplevel by Tk in login.py and add a self.mainloop at the end of the __init__ method.

您可以使用subprocess启动另一个程序,或者重构另一个程序,以便可以将用户名作为函数的参数传递,并将该程序作为主程序的模块导入。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM