【发布时间】:2018-07-17 04:31:34
【问题描述】:
我声明我正在尝试应用此处提出的解决方案Solution by Brad,以便创建更多帧,但我无法输入代码和我在控制台中收到的错误。
import Tkinter as tk
import ttk
import secondpage
TITLE_FONT = ("Helvetica", 18, "bold")
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
self.attributes("-fullscreen", True)
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, secondpage.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):
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="This is the start page", font=TITLE_FONT)
label.place(x=0,y=0,width=592,height=44)
button1 = tk.Button(self, text="Go to Page One",
command=lambda: controller.show_frame(PageOne))
button2 = tk.Button(self, text="Go to Page two",
command=lambda: controller.show_frame(secondpage.PageTwo))
button3 = tk.Button(self, text="Exit",
command=self.quit)
button1.place(x=100,y=406,width=200,height=44)
button2.place(x=300,y=406,width=200,height=44)
button3.place(x=500,y=406,width=80,height=44)
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="This is page one", font=TITLE_FONT)
label.place(x=0,y=0,width=592,height=44)
button1 = tk.Button(self, text="Go to Start Page",
command=lambda: controller.show_frame(StartPage))
#button2 = tk.Button(self, text="Go to Page two",
# command=lambda: controller.show_frame(PageTwo))
button3 = tk.Button(self, text="Exit",
command=self.quit)
button1.place(x=100,y=406,width=200,height=44)
button3.place(x=300,y=406,width=200,height=44)
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
这是我要创建的第二个页面
import Tkinter as tk
import GUIprova
TITLE_FONT = ("Helvetica", 18, "bold")
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="This is page two", font=TITLE_FONT)
label.place(x=0,y=0,width=592,height=44)
button1 = tk.Button(self, text="Go to Start Page",
command=lambda: SampleApp.show_frame(GUIprova.StartPage))
button3 = tk.Button(self, text="Exit",
command=self.quit)
button1.place(x=100,y=406,width=200,height=44)
button3.place(x=300,y=406,width=200,height=44)
这是个例外
Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):
文件 "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", 第 1541 行,在 call 中 返回 self.func(*args) 文件“/Users/Antonello/PycharmProjects/GUI/secondpage.py”,第 19 行,在 command=lambda: SampleApp.show_frame(StartPage)) TypeError: unbound method show_frame() must be called with SampleApp instance as 第一个参数(取而代之的是 classobj 实例)
有人可以帮助我吗?我还应该在第二帧中插入其他按钮,我该怎么办?
【问题讨论】:
-
请缩小您的问题范围并为其创建minimal reproducible example(s)。
-
您正在尝试使用
show_frame,就好像它是一个类方法一样,它不是这样定义的,它是类的实例上的常规方法.所以...command=lambda: SampleApp.show_frame(GUIprova.StartPage))应该改为...command=lambda: controller.show_frame(GUIprova.StartPage))以修复即时错误。 -
谢谢@Nae,但它不起作用。这是一个例外:“文件”/Users/Antonello/PycharmProjects/GUI/GUIprova.py”,第 24 行,在 show_frame frame = self.frames[c] KeyError:
” -
发布的任何代码中都没有 self.frames[c]。
-
@CurlyJoe 那是 untrue 最上面的代码确实引用了它。
标签: python python-2.7 user-interface tkinter