【发布时间】:2017-09-12 23:52:43
【问题描述】:
好的,我需要在 more_example_funcs.py 中获取函数来更改页面。您可以在函数中看到我是如何导入的,然后尝试运行我在 example_gui.py(实际 gui)中的普通代码。这是我用来简化问题的测试脚本,以便我可以学习如何解决它们。
more_example_funcs.py
from Tkinter import *
import Tkinter as tk
import example_gui as EG
def get_text(var):
print var.get()
return EG.self.controller.show_frame(Page_two) ## this will not run
example_gui.py
from Tkinter import *
import Tkinter as tk
# import example_funcs as EF
import more_example_funcs as MEF
class start(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.frames = {}
tk.Tk.title(self, "app name")
menubar = tk.Menu(container)
tk.Tk.config(self, menu=menubar)
fileMenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="Exit", command=quit)
for F in (Page_one, Page_two, Page_three):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(Page_one)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class Page_one(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
self.controller = controller
Frame.__init__(self, parent, *args, **kwargs)
self.labels_one()
self.buttons_one()
self.entries_one()
def labels_one(self):
label1 = Label(self, text="Welcome to page one")
label1.grid()
def buttons_one(self):
button_one = Button(self, text="go to page two", command=lambda:self.controller.show_frame(Page_two))
# window_one_button = Button(self, text="open popup window", command=lambda:EF.popupmsg("New window 1"))
text_one_button = Button(self, text="print entered text", command=lambda:MEF.get_text(self.entry_one_var))
button_one.grid()
# window_one_button.grid()
text_one_button.grid()
def entries_one(self):
self.entry_one_var=StringVar()
entry_one = Entry(self, textvariable= self.entry_one_var)
entry_one.grid()
class Page_two(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
self.controller = controller
Frame.__init__(self, parent, *args, **kwargs)
self.labels_two()
self.buttons_two()
def labels_two(self):
label2 = Label(self, text="Welcome to page two")
label2.grid()
def buttons_two(self):
button_two = Button(self, text="go to page three", command=lambda:self.controller.show_frame(Page_three))
# window_two_button = Button(self, text="open popup window", command=lambda:EF.popupmsg("New window 2"))
button_two.grid()
# window_two_button.grid()
class Page_three(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
self.controller = controller
Frame.__init__(self, parent, *args, **kwargs)
self.labels_three()
self.buttons_three()
def labels_three(self):
label3 = Label(self, text="Welcome to page three")
label3.grid()
def buttons_three(self):
button_three = Button(self, text="go to page one", command=lambda:self.controller.show_frame(Page_one))
# window_three_button = Button(self, text="open popup window", command=lambda:EF.popupmsg("New window 3"))
button_three.grid()
# window_three_button.grid()
if __name__ == "__main__":
app = start()
# EF.center(app)
app.mainloop()
我试图让该函数调用 example_gui.py 中的一个新函数,该函数调用页面进行更改,但我无法正确导入函数。希望你们能帮助完成这项工作并解释它是如何工作的,以便我可以从中学习。
【问题讨论】:
-
忽略我注释掉的东西,这是另一个不想发布和混淆的导入文件。以上所有内容也是可运行的。当点击“打印文本”按钮时页面应该改变
标签: python python-2.7 tkinter