Carry a variable across two files in different classes
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 开发者_如何学Pythonto 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):
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 in UserScore.py
that sets a global variable, then having the function that sets sql
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:
import UserScore
UserScore.user = user
In UserScore.py
, put this near the top:
user = None
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.)
精彩评论