我有以下问题。我尝试访问字典内容,但我收到错误KeyError:'testDatast'。 我在SampleApp中声明了字典。在StartPage类中,创建值为6的“test”键。 如何在Next类中访问此密钥?里面__init__method? 在这种情况下,'testData'键位于Scale小部件的默认值。
import tkinter as tk
from tkinter import font as tkfont
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
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.discUserInfo = {}
self.frames = {}
for F in (StartPage, Next):
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("StartPage")
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.cursor= self.controller.cursor
label = tk.Label(self, text="Login", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
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="Pass:")
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, email,id,name from users Where username = %s AND password = %s"
self.cursor.execute(sql, (username,password_hash))
daneUsera = self.cursor.fetchall()
now = datetime.datetime.now()
srtdata = now.strftime('%Y-%m-%d')
fromData = srtdata + ' 00:00:00'
toData = srtdata + ' 23:59:59'
sql2 = "Select row1,row2,row3 from user_data Where userId = %s and day between %s and %s"
self.cursor.execute(sql2, (daneUsera[0]['id'], fromData, toData))
daneUsera2 = self.cursor.fetchall()
if daneUsera[0]['count'] > 0:
self.controller.discUserInfo['name'] = daneUsera[0]['name']
self.controller.discUserInfo['userId'] = daneUsera[0]['id']
self.controller.discUserInfo['data'] = daneUsera2
self.controller.show_frame("Next")
else:
tm.showerror("Login error", "Incorrect username")
class Next(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Next", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
buttonAudio = tk.Button(self, text="xx",
command=self.ttt, width=20)
buttonAudio.pack()
w = tk.Scale(self, from_=0, to=8, orient='horizontal')
w.pack()
w.set(self.controller.discUserInfo['data']['row1'])
def ttt(self):
print(self.controller.discUserInfo)
if __name__ == "__main__":
app = SampleApp()
app.geometry('{}x{}'.format(800, 650))
app.mainloop()
答案 0 :(得分:2)
在Next.__init__
中设置之前,您运行使用self.controller.discUserInfo['testData']
的{{1}}。在StartPage._login_btn_clicked