[英]Tkinter: how to pass variable from one class (window) to another class (window) - (not toplevel frame)?
In tkinter with python, how do I pass the value of a variable from one class (window) to another class (window), when that class reuses the same frame (it's not a toplevel window). In tkinter with python, how do I pass the value of a variable from one class (window) to another class (window), when that class reuses the same frame (it's not a toplevel window).
For example I have a login window, which I would like to pass the username and account privilege type (Admin, Operator...) to the main window.例如,我有一个登录名 window,我想将用户名和帐户权限类型(管理员、操作员...)传递给主 window。 How to do this?
这个怎么做?
See the example files:请参阅示例文件:
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 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 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()
One of the way is to pass the required value via kwargs
:一种方法是通过
kwargs
传递所需的值:
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
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'
...
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.