Python 3.x 在Python tkinter中的窗口之间传递登录后的用户数据

Python 3.x 在Python tkinter中的窗口之间传递登录后的用户数据,python-3.x,tkinter,Python 3.x,Tkinter,我在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,

我在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显示了这个错误:

打印(self.discUserInfo)属性错误:“Window1”对象没有 属性“discUserInfo”


我试图创建一个全局字典。但仅在某个窗口中工作。

如果您在控制器(SampleApp)中创建字典,则所有其他窗口都可以通过self.controller.discUserInfo访问它。以下内容未经测试,但类似的内容可能会起作用

 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, 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

        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.controller.discUserInfo['name'] ='Joe'
        self.controller.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.controller.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.controller.discUserInfo)

if __name__ == "__main__":
    app = SampleApp()
    app.geometry('{}x{}'.format(800, 650))
    app.mainloop()

作品我非常感激:)没问题,安德西