【发布时间】:2015-11-19 03:16:07
【问题描述】:
我似乎不知道如何在我的应用程序的一个窗口中创建或修改变量,然后在另一个窗口中访问它。另外,如何在 StartPage 中的 spinboxes 上检索选定的值?
这是我正在使用的代码:
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()
【问题讨论】:
标签: python class user-interface variables tkinter