简体   繁体   English

如何将数据保存到变量中

[英]How do I save the data into a variable

I still new in python and have a question here: how could I store the variables outside and to use it later? 我仍然是python的新手,在这里有一个问题:如何将变量存储在外部并在以后使用?

I have already created a class and in that class can store variables, for example, the temp_username , how do I get the temp_username and use it outside of the class? 我已经创建了一个类,并且可以在该类中存储变量,例如temp_username ,如何获取temp_username并在类外使用它?

def read_username(self):
    temp_username = self.username_entry.get()
    temp_username1 = self.username_entry1.get()
    if temp_username == '':
        print("No valid name")
        return
    else:
        self.name_valid = True
        self.username = temp_username
        self.username1 = temp_username1

You are dealing with local variables inside a function. 您正在处理函数内部的局部变量。 What you would need is to transform that local variable into a global one. 您需要将局部变量转换为全局变量。

A way to do that is using parameters and return: 一种方法是使用参数并返回:

def read_username(name):
    temp_username = name
    if temp_username == '':
        print("No valid name")
        return
    else:
        return temp_username

temp_username = read_username("ww")
print(temp_username)

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

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