访问python中类下方法中的变量

发布于 2024-10-25 05:20:45 字数 900 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明天过后 2024-11-01 05:20:45

目前 username 是一个局部变量,无法在方法外部访问。您可以通过执行诸如 self.username 之类的操作来使其成为成员变量。在 __init__ 方法中初始化它可能是一个好主意。

编辑:katrielalex 有正确的想法。您可能希望将用户名存储在不属于用户管理面板的内容中。这可以是一个全局变量,也可以创建一个用户类。

class User:
    def __init__(self, username):
        self.username = username
# Create a global "current_user", with an initial username of None
current_user = User(None)

然后,如果您可以从全局 User 对象访问用户名:

def login(self):
    global current_user
    current_user.username = self.txt_Username.GetValue()

请注意,这都是非常笼统的(例如,这假设您一次只能支持一个用户),但它应该能够帮助您入门。

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.

谁的年少不轻狂 2024-11-01 05:20:45

那么,您需要将用户名的值存储在两个方法都可以看到的地方。最简单的方法,但几乎肯定不是正确的方法,就是将其设为全局变量。您可以使用 global 关键字来做到这一点:

global username
username = ...

但是,几乎可以肯定您更有可能希望将其存储为某个对象的属性。我不了解您的架构,因此无法告诉您最好将其放置在哪里,但像 UserSettings 对象这样的东西会很好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文