class user_management(wx.Panel):
def login(self):
# Login Function can be later integrated to allow users access to your program
username = self.txt_Username.GetValue()
self.frame.SetStatusText("Welcome "+username+" to the system!!")
test = MyApp(0)
test.MainLoop()
How can i access the variable username within another class:
class wizard(wx.wizard.Wizard):
def on_finished(self, evt):
totalscore = self.totalscore
print "Final score: %s" % (totalscore)
user = "c1021358"
db_file="data/data.db"
database_already_exists = os.path.exists(db_file)
con = sqlite3.connect(db_file)
cur = con.cursor()
sql = "INSERT INTO scores (username,totalscore,topic) VALUES ('%s','%s','%s')" % (user,totalscore,tag)
At the moment the user is static, however I want to make this user variable equal to the user开发者_开发百科name taken from the text field box.
Can anyone tell me how I can do this please?
Thank you
Currently username
is a local variable and can't be accessed outside of the method. You could make it a member variable by doing something like self.username
instead. It would probably be a good idea to initialize it in an __init__
method.
EDIT: katrielalex has the right idea. You probably want to store the username in something that is not part of your user_management panel. This could either be a global variable, or you could make a user class.
class User:
def __init__(self, username):
self.username = username
# Create a global "current_user", with an initial username of None
current_user = User(None)
Then if you can access the username from the global User object:
def login(self):
global current_user
current_user.username = self.txt_Username.GetValue()
Please note this is all very general (for instance, this assumes you can only support one user at a time), but it should be able to get you started.
Well, you need to store the value of username
somewhere that both methods can see. The easiest way to do this, but almost certainly not the right one, is to make it a global variable. You can do that using the global
keyword:
global username
username = ...
However, you are almost certainly more likely to want to store it as an attribute of some object. I don't know your architecture so I can't tell you where is best to put it, but something like a User
or a Settings
object would be good.
精彩评论