简体   繁体   English

如何在函数Login()中获取'username'的值,以在另一个python程序上使用它?

[英]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. 我一直试图在函数Login()中获取'username'的值,以在另一个python程序上使用它。

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(). 我需要在函数Login()外使用'Username'的值。 Please provide your insights. 请提供您的见解。

1) Create a python file say ' global_vars.py ' and add this line in it. 1)创建一个说“ global_vars.py ”的python文件,并在其中添加此行。

#global_vars.py
global_V = ''

2) Import this global_vars.py wherever you want set the variable as below: 2)导入该global_vars.py位置,如下所示:

#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 ". 3)考虑您要在文件“ test.py ”中存在的python程序中使用此值。 Just import global_vars.py and use this variable 只需导入global_vars.py并使用此变量

#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. 1)将用户名值写入一个文件,下一个程序在启动时可以读取该文件。

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. 2)第三个运行上面程序的“控制器”或“启动程序”程序,从该程序获取返回值,然后将该值作为参数传递给下一个程序。

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. 如果必须使用python文件,则一个名为main.py的文件包含您的主程序(我假设它是一个GUI程序), 另一个包含登录程序的login.py文件。

main.py 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 login.py

from tkinter import Toplevel, StringVar, Entry, Button, Label 从tkinter导入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. 如果您的主程序没有GUI,请在login.py中用Tk替换Toplevel ,并在__init__方法的末尾添加一个self.mainloop

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python 3 中获取一个函数以返回我可以在另一个函数中使用的值? - How can I get a function in Python 3 to return a value that I can use in another function? 如何在python中的函数内给变量另一个值? - how can I give a variable another value inside a function in python? 如何在python的另一个应用程序中使用函数返回的值? - How can I use a value returned by a function in another application in python? 我如何让这个简单的python登录程序循环? - How can I get this simple python login program to loop? 我如何使用Linux执行命令功能中python程序中存在的变量 - How i can use variable present in python program inside the execute command function of linux 如何在另一个 Python 文件中使用另一个 function 中的 function 的计算值? - How can I use a calculated value of a function in another function in another Python file? 如何在Python程序中使用Jinja2模板? - How can I use a Jinja2 template inside a Python program? Python Django,如何使用用户名(uname)或 email 作为登录凭据? - Python Django, How I Can use username(uname) or email as a login credentials? 我如何在另一个函数中使用一个函数 - How can i use a function inside of another function 如何在另一个python文件中的方法中使用变量的值? - How can I use the value of a variable in a method inside another python file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM