Tkinter:如何将变量从一个 class (window) 传递到另一个 class (window) -(不是顶层框架)?

Tkinter: how to pass variable from one class (window) to another class (window) - (not toplevel frame)?

在带有 python 的 tkinter 中,如何将变量的值从一个 class (window) 传递到另一个 class (window ),当 class 重用相同的框架时(它不是顶层 window)。

例如,我有一个登录 window,我想将用户名和帐户权限类型(管理员、操作员...)传递给主 window。 如何做到这一点?

查看示例文件:

main.py

#!/usr/bin/env python3

from tkinter import *
from frm_login import Wlogin

class Mainframe(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.frame = Wlogin(self)
        self.frame.pack()

    def change(self, frame):
        self.frame.pack_forget() # delete currrent frame
        self.frame = frame(self)
        self.frame.pack() # make new frame


if __name__== '__main__':
    app = Mainframe()
    app.mainloop()

frm_login.py

from tkinter import *
from frm_default import Wmain

class Func(Frame):
    def go_to_main(self, event=None):
        self.myvar = self.entry_var.get() #pass the value of this variable to the class: Wmain
        self.master.change(Wmain)


class Wlogin(Func):
    def __init__(self, master=None, **kwargs):
        Frame.__init__(self, master, **kwargs)

        master.title('First Window')
        master.geometry('300x200')

        self.myvar = StringVar()

        self.entry_var = Entry(self, textvariable=self.myvar)
        self.entry_var.pack()
        self.entry_var.focus()

        self.btn = Button(self, text='Go', command=self.go_to_main)
        self.btn.pack()

frm_default.py

from tkinter import *

class Wmain(Frame):
    def __init__(self, master=None, **kwargs):
        Frame.__init__(self, master, **kwargs)
        master.title('Second Window')
        master.geometry('600x400')

        self.label_var = Label(self, bg='red', fg='#ff0', text='How to show var of first window here?')
        self.label_var.pack()

其中一种方法是通过 kwargs:

传递所需的值
main.py
class Mainframe(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.frame = Wlogin(self)
        self.frame.pack()

    # added **kwargs
    def change(self, frame, **kwargs):
        self.frame.pack_forget() # delete currrent frame
        self.frame = frame(self, **kwargs) ### pass **kwargs
        self.frame.pack() # make new frame
frm_login.py
class Func(Frame):
    def go_to_main(self, event=None):
        self.myvar = self.entry_var.get() #pass the value of this variable to the class: Wmain
        self.master.change(Wmain, data=self.myvar) # pass value via keyword 'data'

...
frm_default.py
class Wmain(Frame):
    def __init__(self, master=None, **kwargs):
        # get the value of keyword argument 'data'
        data = kwargs.pop('data', None)
        Frame.__init__(self, master, **kwargs)
        master.title('Second Window')
        master.geometry('600x400')

        self.label_var = Label(self, bg='red', fg='#ff0', text=data) # use 'data'
        self.label_var.pack()