Python 试图避免使用tkinter和tkinter组合.pack()和.grid()。哪种选择更好?为什么?

Python 试图避免使用tkinter和tkinter组合.pack()和.grid()。哪种选择更好?为什么?,python,tkinter,Python,Tkinter,我一直在使用Python、Tkinter和Pillow(我正在开发的工作应用程序的简单UI)开发GUI。我一直遇到关于几何体管理器的问题,因为您“无法在内部使用几何体管理器网格”。它已经有了由“包”管理的从属服务器 只有当我将我的类页面_花名册添加到应用程序时,才会出现此问题。我不确定我在运行什么,但我怀疑我一定是把\uuu init\uuu声明或实例化搞砸了。感谢您提供的任何帮助,但我最关心的是如何与tkinter中的哪位几何管理员合作,以及何时/为什么 import tkinter as t

我一直在使用Python、Tkinter和Pillow(我正在开发的工作应用程序的简单UI)开发GUI。我一直遇到关于几何体管理器的问题,因为您“无法在内部使用几何体管理器网格”。它已经有了由“包”管理的从属服务器

只有当我将我的
类页面_花名册
添加到应用程序时,才会出现此问题。我不确定我在运行什么,但我怀疑我一定是把
\uuu init\uuu
声明或实例化搞砸了。感谢您提供的任何帮助,但我最关心的是如何与tkinter中的哪位几何管理员合作,以及何时/为什么

import tkinter as tk                
from tkinter import font  as tkfont 
from tkinter import *
import importlib

from PIL import *
from PIL import Image
from PIL import ImageTk

class BellBankLMS(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)


        self.title_font = tkfont.Font(family='Helvetica', size=14, weight="bold", slant="italic")

        self.title('Bell Bank: Learning Management Software')
        self.geometry("450x450")
        self.resizable(0, 0)


        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 (page_Menu, page_Training, page_Quizzes, page_Mgmt, page_Roster):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="nsew")

            self.show_frame("page_Menu")

            # 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.pack(side="top", fill="both", expand=True)

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class page_Menu(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)
        self.controller = controller


        ### This code is not working currently // It displays Bell Bank logo

        # BellLogo = Image.open('bell1.png')
        # BellLogo = BellLogo.resize((85, 85), Image.ANTIALIAS)

        # renderedLogo = ImageTk.PhotoImage(BellLogo)
        # LogoLabel = tk.Label(image=renderedLogo)
        # LogoLabel.pack()


        label = tk.Label(self, text="Bell Bank: Learning Management Software", font=controller.title_font, background="blue", foreground="white")
        label.pack(side="top")

        button1 = tk.Button(self, text="Training",
                            command=lambda: controller.show_frame("page_Training"))
        button2 = tk.Button(self, text="Quizzes",
                            command=lambda: controller.show_frame("page_Quizzes"))
        button3 = tk.Button(self, text="Management",
                            command=lambda: controller.show_frame("page_Mgmt"))
        button1.pack(expand=1)
        button2.pack(expand=1)
        button3.pack(expand=1)


class page_Training(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Training To-Do, Scheduling, etc.", foreground="blue", background="lightgray")
        label.pack(side="top", fill="x", pady=10)

        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("page_Menu"))
        button.pack()


class page_Quizzes(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Quiz Scores, Performance, etc.", foreground="blue", background="lightgray")
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("page_Menu"))
        button.pack()


class page_Mgmt(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Trainer Admin: Course, Roster and Training Mgmt", foreground="blue", background="lightgray")
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("page_Menu"))
        button1 = tk.Button(self, text="Training Classes/Rosters", 
                            command=lambda: controller.show_frame("page_Roster"))
        button.pack()
        button1.pack()


class page_Roster(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)
        self.controller = controller

        tasks=None

        super().__init__()

        if not tasks:
            self.tasks = []
        else:
            self.tasks = tasks

        trainee1 = tk.Label(self, text="---Add Trainee Here---", bg="lightgrey", fg="blue", pady=10)

        self.tasks.append(trainee1)

        for task in self.tasks:
            task.pack(side="top", fill="x")

        self.task_create = tk.Text(self, height=3, bg="white", fg="black")

        self.task_create.pack(side="bottom", fill="x")
        self.task_create.focus_set()

        self.bind("<Return>", self.add_task)

        self.color_schemes = [{"bg": "lightgrey", "fg": "blue"}, {"bg": "grey", "fg": "white"}]


    def add_task(self, event=None):
        task_text = self.task_create.get(1.0,END).strip()

        if len(task_text) > 0:
            new_task = tk.Label(self, text=task_text, pady=10)

            _, task_style_choice = divmod(len(self.tasks), 2)

            my_scheme_choice = self.color_schemes[task_style_choice]

            new_task_configure(bg=my_scheme_choice["bg"])
            new_task_configure(fg=my_scheme_choice["fg"])

            new_task.pack(side="top", fill="x")

            self.tasks.append(new_task)

        self.task_create.delete(1.0, END)


if __name__ == "__main__":
    app = BellBankLMS()
    app.mainloop()
将tkinter作为tk导入
从tkinter导入字体作为tkfont
从tkinter进口*
导入导入库
从PIL进口*
从PIL导入图像
从PIL导入ImageTk
BellBankLMS类(tk.tk):
定义初始化(self,*args,**kwargs):
tk.tk.\uuuuu初始化(self,*args,**kwargs)
self.title\u font=tkfont.font(family='Helvetica',size=14,weight=“bold”,sland=“italic”)
自我名称(“贝尔银行:学习管理软件”)
自几何(“450x450”)
可自行调整大小(0,0)
容器=tk.框架(自身)
container.pack(side=“top”,fill=“both”,expand=True)
container.grid_rowconfigure(0,权重=1)
container.grid\u column配置(0,权重=1)
self.frames={}
对于F in(第页菜单、第页培训、第页测验、第页管理、第页花名册):
页面名称=F.\u名称__
帧=F(父级=容器,控制器=自身)
self.frames[页面名称]=框架
frame.grid(行=0,列=0,sticky=“nsew”)
自我显示框架(“页面菜单”)
#将所有页面放在同一位置;
#在堆叠顺序顶部的那个
#将是可见的。
#frame.pack(side=“top”,fill=“both”,expand=True)
def显示框(自身,页面名称):
''显示给定页面名称''的框架'
frame=self.frames[页面名称]
frame.tkraise()
类页面菜单(tk.Frame):
定义初始化(自、父、控制器):
tk.Frame.\uuuu init\uuuuu(自,父)
self.controller=控制器
###此代码当前不起作用//它显示贝尔银行徽标
#BellLogo=Image.open('bell1.png'))
#BellLogo=BellLogo.resize((85,85),Image.ANTIALIAS)
#renderedLogo=ImageTk.PhotoImage(BellLogo)
#logolable=tk.Label(图像=renderedLogo)
#LogoLabel.pack()
label=tk.label(self,text=“贝尔银行:学习管理软件”,font=controller.title\u font,background=“蓝色”,foreground=“白色”)
标签包装(侧面=“顶部”)
button1=tk.按钮(self,text=“Training”,
command=lambda:controller.show\u frame(“页面培训”))
button2=tk.Button(self,text=“Quizzes”,
command=lambda:controller.show_frame(“页面测验”))
button3=tk.Button(self,text=“Management”,
command=lambda:controller.show\u frame(“页面管理”))
按钮1.包装(展开=1)
按钮2.打包(展开=1)
按钮3.pack(展开=1)
课堂页面培训(传统框架):
定义初始化(自、父、控制器):
tk.Frame.\uuuu init\uuuuu(自,父)
self.controller=控制器
label=tk.label(self,text=“要做的培训、日程安排等”,front=“blue”,background=“lightgray”)
标签包装(侧面=“顶部”,填充=“x”,pady=10)
button=tk.button(self,text=“转到起始页”,
command=lambda:controller.show\u frame(“页面菜单”))
button.pack()
课堂页面测验(传统框架):
定义初始化(自、父、控制器):
tk.Frame.\uuuu init\uuuuu(自,父)
self.controller=控制器
label=tk.label(self,text=“测验分数、成绩等”,前台=“蓝色”,后台=“浅灰色”)
标签包装(侧面=“顶部”,填充=“x”,pady=10)
button=tk.button(self,text=“转到起始页”,
command=lambda:controller.show\u frame(“页面菜单”))
button.pack()
班级页面管理(传统框架):
定义初始化(自、父、控制器):
tk.Frame.\uuuu init\uuuuu(自,父)
self.controller=控制器
label=tk.label(self,text=“培训师管理员:课程、花名册和培训管理”,前台=“蓝色”,后台=“浅灰色”)
标签包装(侧面=“顶部”,填充=“x”,pady=10)
button=tk.button(self,text=“转到起始页”,
command=lambda:controller.show\u frame(“页面菜单”))
button1=tk.按钮(self,text=“培训班/名册”,
command=lambda:controller.show\u frame(“页面花名册”))
button.pack()
按钮1.pack()
班级页面排班(传统框架):
定义初始化(自、父、控制器):
tk.Frame.\uuuu init\uuuuu(自,父)
self.controller=控制器
任务=无
super()。\uuuu init\uuuuu()
如果不是任务:
self.tasks=[]
其他:
self.tasks=任务
培训生1=tk.Label(self,text=“---在此处添加培训生”->,bg=“浅灰色”,fg=“蓝色”,pady=10)
self.tasks.append(受训人员1)
对于self.tasks中的任务:
任务包(side=“top”,fill=“x”)
self.task_create=tk.Text(self,height=3,bg=“白色”,fg=“黑色”)
自我任务\u创建包(side=“bottom”,fill=“x”)
self.task\u create.focus\u set()
self.bind(“,self.add_任务)
self.color_schemes=[{“bg”:“浅灰色”,“fg”:“蓝色”},{“bg”:“灰色”,“fg”:“白色”}]
def add_任务(自身,事件=无):
task\u text=self.task\u create.get(1.0,END.strip)()
如果len(任务文本)>0:
新任务=tk.Label(self,text=task,pady=10)
class page_Roster(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)
        self.controller = controller

        tasks=None

        super().__init__()