У меня проблема с передачей словаря между окнами в Tkinter. После успешного входа в систему я хочу создать словарь, в котором будут храниться данные вошедшего в систему пользователя. Я хотел бы, чтобы словарь был доступен в каждом окне программы. Я пытался сделать это так:
import tkinter as tk # python 3
from tkinter import font as tkfont # python 3
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.frames = {}
for F in (StartPage, Window2, Window1):
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.discUserInfo = {}
label = tk.Label(self, text="Start Page", font=controller.title_font)
label.pack()
label2 = tk.Label(self, text="Login:")
label2.pack()
label2.place()
self.e1 = tk.Entry(self)
self.e1.pack()
self.e1.place()
label3 = tk.Label(self, text="Password:")
label3.pack()
label3.place()
self.e2 = tk.Entry(self, show="*")
self.e2.pack()
self.e2.place()
button1 = tk.Button(self, text="Login",
command=self._login_btn_clicked,width = 25)
button1.pack()
button1.place()
def _login_btn_clicked(self):
### after verifying the login data in database, it creates a dictionary with the user's data ( userId,name,lastName ...)
self.discUserInfo['name'] ='Joe'
self.discUserInfo['userId'] =1
self.controller.show_frame("Window1")
class Window2(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="window 2", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
button1 = tk.Button(self, text="Tset",
command=self.onClick, width=42,bg="#C44041")
button1.pack()
button3 = tk.Button(self, text="Back",
command=lambda : controller.show_frame("Window1"), width=42, bg="#C44041")
button3.pack()
def onClick(self):
print (self.discUserInfo)
class Window1(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="window 1", font=controller.title_font)
label.pack()
button1 = tk.Button(self, text="Next",
command=lambda: controller.show_frame("Window2"), width=42)
button1.pack()
button1.place()
##################################################
button2 = tk.Button(self, text="Test",
command=self.getAlocationData, width=42)
button2.pack()
button2.place()
def getAlocationData(self):
print(self.discUserInfo)
if __name__ == "__main__":
app = SampleApp()
app.geometry('{}x{}'.format(800, 650))
app.mainloop()
Но python показывает эту ошибку:
print (self.discUserInfo) AttributeError: объект 'Window1' не имеет
атрибут «discUserInfo».
Я пытался создать глобальный словарь. Но работал только в некоторых окнах.