Tkinter在多个帧之间切换python

时间:2018-02-07 01:05:15

标签: python python-2.7 user-interface tkinter

我声明我正在尝试应用此处提出的解决方案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回调中的异常回溯(最近一次调用最后一次):
      文件   " /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py" ;,   第1541行,通话       return self.func(* args)File" /Users/Antonello/PycharmProjects/GUI/secondpage.py" ;,第19行,in          command = lambda:SampleApp.show_frame(StartPage))TypeError:必须使用SampleApp实例调用未绑定方法show_frame()   第一个参数(改为使用classobj实例)

有人能帮帮我吗?我还应该在第二帧中插入其他按钮,我该怎么办?

1 个答案:

答案 0 :(得分:0)

将一个函数和一个调用它的按钮添加到其中一个类(PageOne)的示例,但您应该能够自己解决这个问题。

import tkinter as tk
from functools import partial

class SampleApp():
    def __init__(self, root):
        self.root=root
        self.button_dict={}
        self.frame_dict={}
        self.create_buttons()
        self.start_page_frame()

        for classname, lit in [(PageOne, "PageOne"), (PageTwo, "PageTwo")]:
            instance=classname(root)
            self.frame_dict[lit]=instance.page_frame

    def button_press(self, button_id):
        print(button_id)
        ## deselect current frame's button, and activate all others
        for key in self.button_dict:
            if key==button_id:
                self.button_dict[key].config(state="disabled")
            else:
                self.button_dict[key].config(state="normal")

        ## raise the frame corresponding to the button clicked
        self.frame_dict[button_id].lift()

    def create_buttons(self):
        """ you can also use a simple for() to create these
        """
        self.button_frame=tk.Frame(self.root)
        self.button_frame.grid(row=10, column=0)

        button0 = tk.Button(self.button_frame, text="Go to Start Page",
                            bg="lightblue", width=15,
                            command=partial(self.button_press, "StartPage"))
        button0.grid(row=0, column=0, sticky="ew")
        self.button_dict["StartPage"]=button0

        button1 = tk.Button(self.button_frame, text="Go to Page One",
                            bg="yellow", width=15, 
                            command=partial(self.button_press, "PageOne"))
        button1.grid(row=0, column=1, sticky="ew")
        self.button_dict["PageOne"]=button1

        button2 = tk.Button(self.button_frame, text="Go to Page two",
                            bg="lightgreen", width=15,
                            command=partial(self.button_press, "PageTwo"))
        button2.grid(row=0, column=2, sticky="ew")
        self.button_dict["PageTwo"]=button2
        button2.config(state="disabled")

        button_quit = tk.Button(self.button_frame, text="Exit", bg="orange",
                                command=self.root.quit)
        button_quit.grid(row=5, column=0, columnspan=3, sticky="ewns")

    def start_page_frame(self):
        start_frame=tk.Frame(self.root, width=25, height=25)
        tk.Label(start_frame,  width=24, bg="lightblue",
                text="This is the start page").grid(row=0, column=0, sticky="ew")
        start_frame.grid(row=0, column=0, sticky="ns")
        self.frame_dict["StartPage"]=start_frame

class PageOne():
    def __init__(self, parent):
        ## frame must be named self.page_frame to work in for() above
        self.page_frame=tk.Frame(parent)
        self.label_text=tk.StringVar()
        tk.Label(self.page_frame,  width=24,
                         textvariable=self.label_text,
                         bg="yellow").grid(row=0, column=0, sticky="ew")
        self.label_text.set("This is page one")
        self.page_frame.grid(row=0, column=0)

        self.ctr=0
        tk.Button(self.page_frame, text="Change label text",
                  command=self.change_label_text).grid(row=5, column=0)


    def change_label_text(self):
        text_list=["this is page one", "new message page one",
                   "third message for page one"]
        self.ctr += 1
        if self.ctr >= len(text_list):
            self.ctr=0
        self.label_text.set(text_list[self.ctr])

class PageTwo():
    def __init__(self, parent):
        ## frame must be named self.page_frame to work in for() above
        self.page_frame=tk.Frame(parent, width=25, height=25)
        label = tk.Label(self.page_frame,  width=24,
                         text="This is page two",
                         bg="lightgreen").grid(row=0, column=0, sticky="ew")
        self.page_frame.grid(row=0, column=0, sticky="ns")

root=tk.Tk()
SA=SampleApp(root)
root.mainloop()