Он пытается отобразить ползунок, который отвечает за установку возраста вошедшего в систему пользователя. Данные загружаются во время проверки пользователя. Я хочу, чтобы ползунок принял значение по умолчанию self.controller.fromDB ['age']. Но также другие данные, такие как имя или адрес электронной почты пользователя. При попытке использовать словарь self.controller.fromDB ['age'] появляется ключевая ошибка. Разве есть еще одна возможность решить эту проблему?
class FirstApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.userInfo = {}
self.connection = myconnutils.getConnection()
self.cursor = self.connection.cursor()
for F in (LoginPage, MainPage):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("LoginPage")
def __del__(self):
self.connection.close()
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
class LoginPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.cursor= self.controller.cursor
label2 = tk.Label(self, text="Login:")
label2.pack(side="left", fill="x", pady=10)
label2.place(x=310, y=90, in_=self)
self.e1 = tk.Entry(self)
self.e1.pack(side="left", fill="x", pady=10)
self.e1.place(x=370, y=90, in_=self)
label3 = tk.Label(self, text="Password:")
label3.pack(side="left", fill="x", pady=10)
label3.place(x=310, y=120, in_=self)
self.e2 = tk.Entry(self, show="*")
self.e2.pack(side="left", fill="x", pady=10)
self.e2.place(x=370, y=120, in_=self)
button1 = tk.Button(self, text="Login",
command=self._login_btn_clicked,width = 25)
button1.pack()
button1.place(x=310, y=150, in_=self)
def _login_btn_clicked(self):
username = self.e1.get()
password = self.e2.get()
hash = hashlib.sha512()
hash.update(('%s%s' % ('salt', password)).encode('utf-8'))
password_hash = hash.hexdigest()
sql = "Select COUNT(id) AS count,age, from users Where username = %s AND password = %s"
self.cursor.execute(sql, (username,password_hash))
fromDB = self.cursor.fetchall()
if fromDB[0]['count'] > 0:
self.controller.userInfo['age'] = fromDB[0]['age']
self.controller.show_frame("MainPage")
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Main Page", font=controller.title_font)
label.pack()
self.connection = controller.connection
self.cursor = self.controller.cursor
self.w = tk.Scale(self, from_=0, to=200, orient='horizontal')
self.w.pack()
self.w.set(7)
button = tk.Button(self, text="Print",
command=self.funcPrint)
button.pack()
def funcPrint(self):
ee=self.w.get()
print(ee)
print(self.controller.fromDB['age'])
if __name__ == "__main__":
app = FirstApp()
app.geometry('{}x{}'.format(300, 300))
app.mainloop()