【问题标题】:Tkinter switch between multiple frames pythonTkinter在多帧python之间切换
【发布时间】: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


【解决方案1】:

向其中一个类 (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()

【讨论】:

  • 谢谢,但如果我想在另一个 python 文件中创建另一个类(例如“第一帧”),我应该如何进行?我没有成功,这是 guy.py codepaste.net/4cncnr 这是 FirstFrame.py codepaste.net/6qwtei
猜你喜欢
  • 1970-01-01
  • 2020-10-10
  • 2011-11-24
  • 1970-01-01
  • 2016-06-07
  • 1970-01-01
  • 2018-09-24
  • 1970-01-01
相关资源
最近更新 更多