这是我程序中的一个类,我想从该类中获取变量配置文件,并在另一个类中使用它
class LoginPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
self.Logo = PhotoImage(file = "Logo.gif")
def Log_in():
with sqlite3.connect("UserInfo.db") as db:
cursor = db.cursor()
user = (self.UserEntry.get())
Pass = (self.PassEntry.get())
if user == "" or Pass == "":
msg = MsgBox("", "Make sure to fill all the boxes\nPlease try again")
msg.label['font'] = 'Verdana 10'
msg.button['text'] = 'Close'
msg.button.pack(expand = True)
else:
dictionary = {}
cursor.execute("SELECT username, password FROM users")
for pword in cursor.fetchall():
(key ,val) = tuple(pword)
dictionary[str(key)] = val
if user in dictionary:
if dictionary[user] == Pass:
sql = '''SELECT userID FROM users WHERE username = ?'''
cursor.execute(sql, (user,))
***profile = cursor.fetchall()***
controller.show_frame(HomePage)
self.UserEntry.delete(0, 'end')
self.PassEntry.delete(0, 'end')
else:
messagebox.showinfo("", "Enter the correct password")
self.PassEntry.delete(0, 'end')
self.logo = tk.Label(self, image = self.Logo)
self.User = tk.Label(self, text = "User Name:").place(x = 72, y = 130)
self.Pass = tk.Label(self, text = "Password:").place(x = 80, y = 155)
self.UserEntry = Entry(self)
self.PassEntry = Entry(self, show = '*')
self.login = ttk.Button(self, text = "Login", command = Log_in)
self.signup = ttk.Button(self, text = "Create account",
command = lambda:controller.show_frame(SignUp))
self.UserEntry.place(width = 100, height = 20, x = 140, y = 130)
self.PassEntry.place(width = 100, height = 20, x = 140, y = 155)
self.login.place(x = 80, y = 180)
self.signup.place(x = 160, y = 180)
self.logo.place(x = 110, y = 20)
下面的是我想将变量概要文件放入的类。
class AddJob(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
def dbAddJob():
JobT = self.Jt.get("1.0", 'end').rstrip('\n')
JobD = self.Jd.get("1.0", 'end').rstrip('\n')
with sqlite3.connect("UserInfo.db") as db:
cursor = db.cursor()
if JobT == "" or JobD == "":
messagebox.showinfo("Invalid","please fill in the boxes or cancel")
else:
aj = messagebox.askokcancel("Continue", "Job added click ok to continue\nor cancel or change what you have added")
if aj == True:
cursor.execute("""
INSERT INTO jobs (jobtitle, jobdescript)
VALUES (?, ?)
""", (JobT, JobD))
db.commit()
self.Jt.delete('1.0', END)
self.Jd.delete('1.0', END)
controller.show_frame(JobsPage)
cursor.execute("SELECT * FROM jobs")
print(cursor.fetchall())
MapPageCanvas = Canvas(self, width = 320, height = 568)
MapPageCanvas.pack()
TopBar = MapPageCanvas.create_rectangle(0, 0, 320, 50,
fill = 'light grey')
Home = MapPageCanvas.create_text((100, 25),
text = "Add a Job",
font = HOME_FONT)
MenuButton = ttk.Button(self, text = "Back",
command = lambda:controller.show_frame(JobsPage)).place(height = 40, width = 40, x = 5, y = 5)
self.Addjobbutton = ttk.Button(self, text = "Add",
command = dbAddJob).place(width = 60, height = 30, x = 90, y = 520)
self.Cancel = ttk.Button(self, text = "cancel",
command = lambda:controller.show_frame(JobsPage)).place(width = 60, height = 30, x = 170, y = 520)
self.Jt = ScrolledText(self)
self.Jd = ScrolledText(self)
self.Jt.place(height = 30, width = 310, x = 5, y = 60)
self.Jd.place(height = 400, width = 310, x = 5, y = 100)
如果有人给我一些建议,我会很感激
答案 0 :(得分:0)
阅读代码中的注释,它应该很简单。 如有任何疑问,请询问。
class LoginPage():
def __init__(self):
# Create empty variable
self.profile = ""
def Log_in(self):
# Set profile value
self.profile = "Hello World!"
def returnProfile(self):
# return profile
return self.profile
# Create Object
page = LoginPage()
# Return Profile Data
profile = page.returnProfile()
print(profile)
// Hello World!
此处提供有关OOP的更多信息:https://www.tutorialspoint.com/python/python_classes_objects.htm