访问python中类下方法中的变量
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()
我如何访问另一个类中的变量用户名:
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)
目前用户是静态的,但是我想使该用户变量等于从文本字段框中获取的用户名。
谁能告诉我该怎么做?
谢谢
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 username taken from the text field box.
Can anyone tell me how I can do this please?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前
username
是一个局部变量,无法在方法外部访问。您可以通过执行诸如self.username
之类的操作来使其成为成员变量。在 __init__ 方法中初始化它可能是一个好主意。编辑:katrielalex 有正确的想法。您可能希望将用户名存储在不属于用户管理面板的内容中。这可以是一个全局变量,也可以创建一个用户类。
然后,如果您可以从全局 User 对象访问用户名:
请注意,这都是非常笼统的(例如,这假设您一次只能支持一个用户),但它应该能够帮助您入门。
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 likeself.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.
Then if you can access the username from the global User object:
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.
那么,您需要将用户名的值存储在两个方法都可以看到的地方。最简单的方法,但几乎肯定不是正确的方法,就是将其设为全局变量。您可以使用
global
关键字来做到这一点:但是,几乎可以肯定您更有可能希望将其存储为某个对象的属性。我不了解您的架构,因此无法告诉您最好将其放置在哪里,但像
User
或Settings
对象这样的东西会很好。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 theglobal
keyword: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 aSettings
object would be good.