尽管处于不同框架中,但网格和包装发生冲突

时间:2019-03-01 14:53:20

标签: python tkinter

我正在创建一个使用混合框架来帮助组织的应用程序。我总共使用了3个,它们分别排列在左侧,右侧和顶部。然后,我最初使用网格来组织其中的小部件。但是,我打包到右侧的sub_frame打包在打包到顶部的另一个sub_frame的顶部。我不知道为什么会这样。但是,我尝试在sub_frames之一中使用pack使其更简单。然后我遇到了这个错误:

Traceback (most recent call last):
  File "E:/Python/Py_Proj/Attempt at treeview.py", line 78, in <module>
    app = App(window)
  File "E:/Python/Py_Proj/Attempt at treeview.py", line 42, in __init__
    self.Banner_Label.grid(row=0,column=0)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 2223, in grid_configure
    + self._options(cnf, kw))
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack

代码如下:

import tkinter as tk
from tkinter import ttk

user_list = [("foo", 3), ("bar", 3), ("Terri", 2), ("Chris", 2),
             ("Tony", 2), ("Emma", 2), ("Thomas", 1), ("Penny", 1),
             ("Kamryn", 1), ("Kelsie", 1), ("James", 1), ("Connor", 1),
             ("Steve", 1), ("Bob", 2), ("Elon", 1), ("Albert", 1),
             ("Brian", 1), ("Sam", 1), ("Karen", 1), ("Sophie", 1),
             ("Leo", 1), ("Alfie", 1), ("Alan", 1), ("Turing", 3),
             ("Reeves", 2), ("Greene", 2), ("Seal", 2), ("Keeble", 1),
             ("Bailey", 1), ("Kaey", 1), ("Joe", 1), ("Lauren", 1),
             ("Lasagna", 1), ("Morris", 1), ("Quinn", 1), ("Mitchell", 2)]

class App(tk.Frame):
    def __init__(self,master):
        tk.Frame.__init__(self,master)
        self.master = master

        # Tree Frame
        self.Tree_Frame = tk.Frame(self, width=200, height=200).pack(side=tk.LEFT)
        self.Tree = ttk.Treeview(self.Tree_Frame, columns=("a", "b"))
        self.Tree['show'] = 'headings'
        self.Tree["columns"] = ("User", "Clearance")
        self.Tree.heading("User", text="Name")
        self.Tree.heading("Clearance", text="Clearance")
        self.Tree.column("User", width=100, anchor="center")
        self.Tree.column("Clearance", width=80, anchor="center")


        # Func Frame
        self.Func_Frame = tk.Frame(self, width=200, height=200).pack(side=tk.RIGHT)
        self.Func_Title_Var = tk.StringVar()
        self.Func_Title = ttk.Label(self.Func_Frame, textvariable=self.Func_Title_Var).pack()
        self.Func_Entry_Var = tk.StringVar()
        self.Func_Entry_Box = ttk.Entry(self.Func_Frame, textvariable=self.Func_Entry_Var).pack()
        self.Func_Text_Box = tk.Text(self.Func_Frame, width=20, height=3).pack()
        self.Func_Submit = ttk.Button(self.Func_Frame, text="Submit", command=lambda: print("Submit_Button")).pack()

        # Banner Frame
        self.Banner_Frame = tk.Frame(self,width=200,height=200).pack(side=tk.TOP)
        self.Banner_Label = ttk.Label(self.Banner_Frame,text="AssignmentPage")
        self.Banner_Label.grid(row=0,column=0)
        self.Banner_Reset = ttk.Button(self.Banner_Frame,text="Reset",
                                       command=lambda: print("Reset_Button"))
        self.Banner_Reset.grid(row=0,column=1)
        self.Banner_Back = ttk.Button(self.Banner_Frame, text="Back",
                                      command=lambda:print("Back_Button"))
        self.Banner_Back.grid(row=0, column=2)
        self.Banner_Add = ttk.Button(self.Banner_Frame,text="Add Assignment",
                                     command=lambda:print("Add_Assignment"))
        self.Banner_Add.grid(row=1,column=0)
        self.Banner_Edit = ttk.Button(self.Banner_Frame,text="Edit Assignment",
                                      command=lambda: print("Edit_Assignment"))
        self.Banner_Edit.grid(row=1,column=1)
        self.Banner_Remove = ttk.Button(self.Banner_Frame,text="Delete Assignment",
                                        command=lambda: print("Delete_Assignment"))
        self.Banner_Remove.grid(row=1,column=2)

        for user in user_list:
            self.Tree.insert("", "end", values=user)
        self.Tree.bind("<ButtonRelease-1>", self.Select_Item)
        self.Tree.pack()

    def Select_Item(self,event):
        Current_Item = self.Tree.item(self.Tree.focus())
        Row_Value = Current_Item["values"]
        print("Row_Value =",Row_Value)


if __name__ == "__main__":
    window = tk.Tk()
    app = App(window)
    window.mainloop()

1 个答案:

答案 0 :(得分:4)

问题是这一行:

self.Banner_Frame = tk.Frame(self,width=200,height=200).pack(side=tk.TOP)

您需要横幅框架小部件的引用,该引用应为:

self.Banner_Frame = tk.Frame(self,width=200,height=200)
self.Banner_Frame.pack(side=tk.TOP)

您现在正在做的是将None传递为小部件的母版。

我还注意到您将master作为参数,但没有打包App框架。如果是这样,则在创建框架时还需要传递master

self.Tree_Frame = tk.Frame(master, width=200, height=200)
....
self.Func_Frame = tk.Frame(master, width=200, height=200)
....
self.Banner_Frame = tk.Frame(master,width=200,height=200)

或者,只需执行self.pack()