[英]In tkinter, how can I access variables created in one window in a different one?
我似乎无法弄清楚如何在我的应用程序的一个窗口中创建或修改变量,然后在另一个窗口中访问它。 另外,如何在StartPage的Spinbox上检索选定的值?
这是我正在使用的代码:
import tkinter as tk
TITLE_FONT = ("Helvetica", 18, "bold")
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
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, PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, c):
'''Show a frame for the given class'''
frame = self.frames[c]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="CALCULO DE PRECIOS", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
test1 = tk.Spinbox(self, values=(1, 2, 4, 8))
test1.pack()
test2 = tk.Spinbox(self, values=(1, 2, 4, 8))
test2.pack()
button1 = tk.Button(self, width=20, text="Calculo Final",
command=lambda: controller.show_frame(PageOne))
button2 = tk.Button(self, width=20, text="Calculo Actividades",
command=lambda: controller.show_frame(PageTwo))
button1.pack()
button2.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="CALCULO FINAL", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, width=20, text="Volver al menu principal",
command=lambda: controller.show_frame(StartPage))
button.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="CALCULO ACTIVIDADES", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, width=20, text="Volver al menu principal",
command=lambda: controller.show_frame(StartPage))
button.pack()
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
由于您创建的每个窗口都是具有对控制器引用的类的实例,因此可以通过在控制器上设置一个值在窗口之间传递变量。
例如,在从PageOne
创建的窗口中,您可以像这样在控制器上设置一个值:在init
,确保将对控制器的引用存储在PageOne
和PageTwo
类中:
self.controller = controller
在要传递变量时,可以在控制器对象上进行设置:
self.controller.value = "value"
在通过PageTwo
创建的窗口中,您可以像这样访问此值:
value_from_other_window = self.controller.value
要回答您的另一个问题,要从旋转框访问值,您需要对旋转框对象进行引用。 我建议您将这些保留为PageOne
和PageTwo
类的实例变量:
self.test1 = tk.Spinbox(self, values=(1, 2, 4, 8))
需要访问值时,可以调用:
self.test1.get()
您可能不应该拥有两个类PageOne
和PageTwo
因为它们似乎都具有完全相同的代码,只是标签文本。 您可以考虑向init添加另一个参数,即标签文本。
我已修改您的代码,以获取具有Spinbox小部件特性的StartPage类中Spinbox的选定值,并在按钮回调中的PageOne和PageTwo类中输出当前值:
import tkinter as tk
TITLE_FONT = ("Helvetica", 18, "bold")
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
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.varSpin1=1
self.varSpin2=1
self.frames = {}
for F in (StartPage, PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, c):
'''Show a frame for the given class'''
frame = self.frames[c]
frame.tkraise()
#print("spinbox values: %d %d"%(self.varSpin1,self.varSpin2))
class StartPage(tk.Frame):
def setVarSpin1(self,v):
self.controller.varSpin1=v
def setVarSpin2(self,v):
self.controller.varSpin2=v
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="CALCULO DE PRECIOS", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
self.controller=controller
val1 = tk.IntVar()
val2 = tk.IntVar()
self.test1 = tk.Spinbox(self, values=(1,2,4,8), textvariable=val1,
command=lambda:self.setVarSpin1(val1.get())).pack()
self.test2 = tk.Spinbox(self, values=(1,2,4,8), textvariable=val2,
command=lambda:self.setVarSpin2(val2.get())).pack()
button1 = tk.Button(self, width=20, text="Calculo Final",
command=lambda: controller.show_frame(PageOne)).pack()
button2 = tk.Button(self, width=20, text="Calculo Actividades",
command=lambda: controller.show_frame(PageTwo)).pack()
class PageOne(tk.Frame):
def callback(self):
print("PageOne: spinbox values: %d %d"%(self.controller.varSpin1,self.controller.varSpin2))
self.controller.show_frame(StartPage)
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="CALCULO FINAL", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, width=20, text="Volver al menu principal",
command=lambda:self.callback()).pack()
self.controller=controller
class PageTwo(tk.Frame):
def callback(self):
print("PageTwo: spinbox values: %d %d"%(self.controller.varSpin1,self.controller.varSpin2))
self.controller.show_frame(StartPage)
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="CALCULO ACTIVIDADES", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, width=20, text="Volver al menu principal",
command=lambda:self.callback()).pack()
self.controller=controller
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.