如何在tkinter for python中保存共享数据?
我对Python的GUIs非常陌生,并试图用多个页面构建我的第一个gui,但是共享一个输入框中的变量确实让我陷入了一个循环。我知道代码可能有很多错误,但现在,我真的想更好地了解如何从用户名输入框在页面之间共享变量 以下是与此相关的代码:(分页符正好是一些不相关的代码) 我知道检索函数看起来很时髦,可能一点也不正确,但我已经在这个特定问题上工作了大约一个星期了,它已经把我引向了一些野兔洞 最终目标是如何在tkinter for python中保存共享数据?,python,tkinter,Python,Tkinter,我对Python的GUIs非常陌生,并试图用多个页面构建我的第一个gui,但是共享一个输入框中的变量确实让我陷入了一个循环。我知道代码可能有很多错误,但现在,我真的想更好地了解如何从用户名输入框在页面之间共享变量 以下是与此相关的代码:(分页符正好是一些不相关的代码) 我知道检索函数看起来很时髦,可能一点也不正确,但我已经在这个特定问题上工作了大约一个星期了,它已经把我引向了一些野兔洞 最终目标是pageTwo的label1显示“欢迎,(插入startPage的entry1中输入的电子邮件”)
pageTwo
的label1
显示“欢迎,(插入startPage
的entry1
中输入的电子邮件”)
我认为我的问题在于pageTwo
从shared_data
检索空字符串,但我不明白为什么会这样
非常感谢您的帮助 我想问题是因为帧是在
Keep.\uuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
您应该在\uuuu init\uuuuuu
中创建空标签,并在其他方法(即update\u widgets()
)中创建文本Welcome…
,如果所有类都有update\u widgets()
),您将在show\u frame()
或内事件之后执行该方法
最低工作代码:
import tkinter as tk
class Keep(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.shared_data ={
"email": tk.StringVar(),
"password": tk.StringVar()
}
self.frames = {
'StartPage': StartPage(self, self),
'PageTwo': PageTwo(self, self),
}
self.current_frame = None
self.show_frame('StartPage')
def show_frame(self, name):
if self.current_frame:
self.current_frame.forget()
self.current_frame = self.frames[name]
self.current_frame.pack()
self.current_frame.update_widgets() # <-- update data in widgets
class StartPage(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
self.entry1 = tk.Entry(self, textvariable=self.controller.shared_data["email"])
self.entry1.pack()
entry2 = tk.Entry(self, show='*')
entry2.pack()
button = tk.Button(self, text="Submit", command=lambda:controller.show_frame("PageTwo"))
button.pack()
def update_widgets(self):
pass
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
self.label = tk.Label(self, text="") # <-- create empty label
self.label.pack()
def update_widgets(self):
self.label["text"] = "Welcome, {}".format(self.controller.shared_data["email"].get()) # <-- update text in label
if __name__ == "__main__":
keep = Keep()
keep.mainloop()
将tkinter作为tk导入
班级管理(tk.tk):
定义初始化(self,*args,**kwargs):
super()
自共享数据={
“电子邮件”:tk.StringVar(),
“密码”:tk.StringVar()
}
self.frames={
“起始页”:起始页(self,self),
“第二页”:第二页(自我,自我),
}
self.current\u frame=无
自显示帧(“起始页”)
def显示_帧(自身,名称):
如果self.current\u帧:
self.current_frame.forget()
self.current_frame=self.frames[名称]
self.current_frame.pack()
self.current_frame.update_widgets()#在调用.retrieve()之前显示PageTwo
,因此,初始化页面时检索到的数据不可用。您删除了太多行,现在我们无法运行它来查看问题。因为这些类在上非常流行,所以我可以猜测问题是因为类是在Keep中开始创建的。uuu init_uuu
soPageTwo。uu init_uu
在开始时执行,运行show_-frame
时不会,因此Welcome,
是在开始时创建的,而不是在运行show_-frame()
时创建的。在更新标签/条目中文本的类中应该有方法update\u widgets()
,如果所有类都有update\u widgets()
,则应该在show\u frame()
之后运行该方法update\u widgets()
,或者在show\u frame()内部运行update\u widgets()
。不要使用名称update()
,因为tkinter
已经使用了这个名称。非常感谢!代码符合我现在的要求。我没有提供大量的代码,因为它确实需要一些清理,但那个特定的部分正在蚕食我。
import tkinter as tk
class Keep(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.shared_data ={
"email": tk.StringVar(),
"password": tk.StringVar()
}
self.frames = {
'StartPage': StartPage(self, self),
'PageTwo': PageTwo(self, self),
}
self.current_frame = None
self.show_frame('StartPage')
def show_frame(self, name):
if self.current_frame:
self.current_frame.forget()
self.current_frame = self.frames[name]
self.current_frame.pack()
self.current_frame.update_widgets() # <-- update data in widgets
class StartPage(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
self.entry1 = tk.Entry(self, textvariable=self.controller.shared_data["email"])
self.entry1.pack()
entry2 = tk.Entry(self, show='*')
entry2.pack()
button = tk.Button(self, text="Submit", command=lambda:controller.show_frame("PageTwo"))
button.pack()
def update_widgets(self):
pass
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
self.label = tk.Label(self, text="") # <-- create empty label
self.label.pack()
def update_widgets(self):
self.label["text"] = "Welcome, {}".format(self.controller.shared_data["email"].get()) # <-- update text in label
if __name__ == "__main__":
keep = Keep()
keep.mainloop()