[英]How to access variables from different class in tkinter to use in different functions?
我在访问不同 class 的数据时遇到问题。 我在这个平台上找到了一些解决方案,但我仍然无法在我的脚本中实现它。 我将感谢您对此的帮助。
我试图设计一个登录页面,包括用户名和密码。 用户填写这些字段并单击登录按钮后,我想获取这些变量(从第 1 页开始)以用于不同的功能(在 def printName()
中打印)。
class Page(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
def show(self):
self.lift()
class Page1(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs,bg='white')
label = tk.Label(self, text="",bg='white')
heading= tk.Label(self, text="ENTER OM", font=("ALGERIAN",40,"bold"), fg="black",bg='white').pack()
user_name= tk.Label(self, text="User Name :", font=("arial",20,"bold"), fg="black",bg='white').place(x=15,y=201)
password= tk.Label(self, text="Password :", font=("arial",20,"bold"), fg="black",bg='white').place(x=15,y=305)
self.shared_data = {"last_user_name": tk.StringVar(),"password": tk.StringVar()}
entry1 = tk.Entry(self, textvariable=self.shared_data["last_user_name"]).place(x=210, y=214)
last_user_name = self.shared_data["last_user_name"].get()
button_1=tk.Button(self,text="Log In", width=12,height=3,fg="white",bg="blue",font=("Arial",10), command=printName).place(x=350,y=450)
button_3=tk.Button(self,text="Close", width=12,height=3,fg="white", bg="red", font=("Arial",10),command=close_window).place(x=180,y=450)
label.pack(side="top", fill="both", expand=True)
class Page2(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
label = tk.Label(self, text="", bg="white")
button_2=tk.Button(self,text="Om Creation", width=10,height=3 ,fg="white",bg="blue",font=("Arial",10), command=OMcreat).place(x=750,y=400)
label.pack(side="top", fill="both", expand=True)
class Page3(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
label = tk.Label(self, text="")
label.pack(side="top", fill="both", expand=True)
class MainView(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
p1 = Page1(self)
p2 = Page2(self)
p3 = Page3(self)
buttonframe = tk.Frame(self)
container = tk.Frame(self)
root.configure(bg='white')
buttonframe.pack(side="top", fill="x", expand=False)
container.pack(side="top", fill="both", expand=True)
p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
p3.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
b1 = tk.Button(buttonframe, text="Page 1", command=p1.lift)
b2 = tk.Button(buttonframe, text="Page 2", command=p2.lift)
b3 = tk.Button(buttonframe, text="Page 3", command=p3.lift)
b1.pack(side="left")
b2.pack(side="left")
b3.pack(side="left")
p1.show()
if __name__ == "__main__":
root = tk.Tk()
root.title("OM credantials")
root.wm_geometry("1000x750+0+0")
root.configure(bg='white')
main = MainView(root)
main.pack(side="top", fill="both", expand=True)
root.mainloop()
def close_window():
root.destroy()
driver.close()
def printName():
print(last_user_name)
您可以使用全局变量或只保存 p1 class:
logvar=''
def save_login(l):
global logvar
logvar=l
def get_last_login():
return logvar
或者使用self.p1
代替 MainView 中的p1
和main.p1.shared_data['last_user_name']
第二种方法更好。 一般不推荐使用全局变量。
最后将评估printName
和close_window
方法,而不是在 class 之外声明它们,将它们移动到Page1
并更改按钮命令:
class Page1(Page):
def __init__(self, *args, **kwargs):
...
button_1 = tk.Button(..., command=self.printName)
button_3 = tk.Button(..., command=self.close_window)
def printName(self):
print(self.last_user_name.get())
def close_window(self):
root.destroy()
driver.close()
您有一个NameError: name 'OMcreat' is not defined
in Page2
。
class Page2(Page):
def __init__(self, *args, **kwargs):
...
button_2 = tk.Button(..., command=self.OMcreat)
button_2.place(x=750,y=400)
def OMcreat(self):
pass
在用户填写之前, last_user_name
的值应该为空。 将其替换为
self.last_user_name = self.shared_data["last_user_name"]
并在需要时使用get
方法。
heading
变量将是None
类型,它是pack
方法的结果(与place
方法相同),因此将其更改为以下内容并对其他变量执行相同操作:
heading= tk.Label(...)
heading.pack()
编辑
MainView 实例的引用已经在args
中可用。要访问页面实例,您可以将它们定义为实例变量:
self.p1 = Page1(self)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.