多个帧的Tkinter示例,网格错误

时间:2015-06-14 01:40:49

标签: python tkinter

我正在使用以下示例,在此门户网站上找到。并尝试添加列表框。我知道我可以使用pack,而不是网格,但打包非常混乱,并且使用网格我可以更具体。

 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="This is the start page",  font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)

        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(PageTwo))
        button1.pack()
        button2.pack()
 class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="This is page 1", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           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="This is page 2", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame(StartPage))
        button.pack()
 if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

例如,我想将此添加到PageTwo类。但我不想使用.pack。

    lbox = tk.Listbox(self, height=10)
    lbox.grid(column=0, row=0, padx=5)
    s = ttk.Scrollbar(self, orient="vertical", command=lbox.yview)
    s.gird(column=1, row=1, sticky=("s","e"))

我收到此错误

C:\Python34\python.exe C:/Users/Mariusz/OneDrive/Projekty/Python/himan/main.py
Traceback (most recent call last):
  File "C:/Users/Mariusz/OneDrive/Projekty/Python/himan/main.py", line 198, in <module>
    app = Himan()
  File "C:/Users/Mariusz/OneDrive/Projekty/Python/himan/main.py", line 44, in __init__
    frame = F(container, self)
  File "C:/Users/Mariusz/OneDrive/Projekty/Python/himan/main.py", line 132, in __init__
    lbox.grid(column=0, row=0, padx=5)
  File "C:\Python34\lib\tkinter\__init__.py", line 2057, in grid_configure
    + self._options(cnf, kw))
_tkinter.TclError: cannot use geometry manager grid inside .52613520.52708464 which already has slaves managed by pack

Process finished with exit code 1

2 个答案:

答案 0 :(得分:0)

您不能在同一个容器中混合使用packgrid可能将它们混合在不同的容器中,但我高度建议继续使用 这些几何管理器中的一个。

请参阅此question以获取更多帮助。

答案 1 :(得分:0)

只要您在pack内仅使用gridPageTwo,就可以了。正如错误消息告诉您的那样,您尝试同时使用它们。如果您想在该课程中使用grid,请确保仅为grid的孩子使用 PageTwo