在不同类的两个文件之间携带变量
我想在一个人登录后将变量 user 携带到另一个文件中,该文件将填充或携带该用户变量,以便我可以在 SQL 语句中使用它。
def ViewScore(self, event):
root = Tk()
root.title("You last 5 attempts for this topic")
user = user_management.getuser()
app = UserScore(root)
root.mainloop()
变量 user 可以工作,我使用打印语句对其进行了测试,但是我需要将其转移到 UserScore.py 文件并运行 SQL 语句。
sql = 'SELECT * FROM scores where username="%s" and topic="binary" order by ID DESC' % (user)
sql 语句中的用户变量似乎永远不会被填充或创建,我只是不知道该怎么做。
用户评分.py
class UserScore(Frame):
def __init__(self,master):
Frame.__init__(self,master)
self.pack()
self.getRecords()
def getRecords(self):
I want to carry the variable user once a person is logged in, to another file that will populate or carry over that user variable so that I can use it in an SQL Statement.
def ViewScore(self, event):
root = Tk()
root.title("You last 5 attempts for this topic")
user = user_management.getuser()
app = UserScore(root)
root.mainloop()
The variable user works, ive tested it using the print statments, however I need to carry it over to the UserScore.py file and run an SQL statement.
sql = 'SELECT * FROM scores where username="%s" and topic="binary" order by ID DESC' % (user)
The user variable there in the sql statement seems to never get populated or created, I just dont know how to do it.
UserScore.py
class UserScore(Frame):
def __init__(self,master):
Frame.__init__(self,master)
self.pack()
self.getRecords()
def getRecords(self):
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜测设置 sql 的函数是某种 GUI 回调,而不是您直接使用参数调用的函数。您是否尝试过在 UserScore.py 中调用设置全局变量的函数,然后让设置 sql 的函数访问该全局变量?
可能不是最 Pythonic 的方式来做到这一点 - 可能应该使用类实例来做到这一点 - 但它应该有效并且很简单。
编辑:澄清一下:
在可以访问用户的函数中,添加以下内容:
在
UserScore.py
中,将其放在顶部附近:然后在 UserScore.UserScore 函数中,只需访问变量
user
。(给模块和类赋予相同的名称和相同的大小写可能不是一个好主意,可能会造成混乱。)
I'm guessing the function that sets
sql
is some kind of GUI callback, not something you'd be calling directly with arguments. Have you tried calling a function inUserScore.py
that sets a global variable, then having the function that setssql
access that global variable?Probably not the most Pythonic way to do it - should probably do it with a class instance - but it should work and it's simple.
Edit: To clarify:
In the function that can access user, add this:
In
UserScore.py
, put this near the top:Then in the UserScore.UserScore function, just access the variable
user
.(Probably not a good idea to give the module and class the same name with same capitalization, can get confusing.)