首页 > 解决方案 > 添加框架来组织我的程序没有什么困难

问题描述

我正在使用此代码在我的 Tkinter 文本编辑器应用程序中的帧之间导航。当我想用多个框架组织页面以收集同一框架中的所有按钮和其他一些可能的框架中的其他小部件时,我得到了一个错误:_tkinter.TclError: cannot use geometry manager grid inside。它已经有由包管理的奴隶。如果你能帮助我,泰。

这是我使用的代码

import tkinter as tk
from tkinter import ttk

LARGE_FONT=("Verdana",12)

def popupmsg(msg):
    popup=tk.Tk()
    popup.wm_title("!")
    label=ttk.Label(popup,text=msg,font=LARGE_FONT)
    label.pack()
    b1=ttk.Button(popup,text="OKAY",command=popup.destroy)
    b1.pack()
    popup.mainloop()


class MainWindow(tk.Tk):

    def __init__(self,*arg,**kwargs):

        tk.Tk.__init__(self,*arg,**kwargs)

        container=tk.Frame(self)
        container.pack(side="top",fill="both",expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        menubar=tk.Menu(container)
        filemenu=tk.Menu(menubar,tearoff=0)
        filemenu.add_command(label="Save settings", command=lambda:popupmsg("Not supported yet!"))
        filemenu.add_command(label="Exit",command=quit)
        menubar.add_cascade(label="File", menu=filemenu)
        tk.Tk.config(self,menu=menubar)




        self.frames={}
        for F in(StartPage,PageOne):
            frame=F(container,self)
            self.frames[F]=frame
            frame.grid(row=0,column=0,sticky="nsew")
            self.show_frame(StartPage)

    def show_frame(self,cont):
        frame=self.frames[cont]
        frame.tkraise()

class StartPage(tk.Frame):

    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)
        label_startpage=ttk.Label(self,text="Start Page",font=LARGE_FONT)
        label_startpage.pack(padx=10,pady=10)
        button_to_pageONE=ttk.Button(self,text="Go to Page ONE",command= lambda: 
controller.show_frame(PageOne)).pack()


class PageOne(tk.Frame):

    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)
        self.rowconfigure(0, minsize=200, weight=1)
        self.columnconfigure(1, minsize=200, weight=1)

        txt_edit = tk.Text(self).grid(row=0, column=1, sticky="nsew")
        button_frame = tk.Frame(self,bg="lightblue").grid(row=0, column=0, sticky="ns")
        btn_open = ttk.Button(button_frame, text="Open").grid(row=0, column=0, sticky="ew", padx=5, pady=5)
        btn_save = ttk.Button(button_frame, text="Save As...").grid(row=1, column=0, sticky="ew", padx=5)
        button_to_startpage = ttk.Button(button_frame, text="Back to Start Page",
                                     command=lambda: 
controller.show_frame(StartPage)).grid(row=2, column=0,
                                                                                            
sticky="ew", padx=5,
                                                                                             
pady=5)





app=MainWindow()
app.geometry("1280x720")
app.mainloop()

标签: pythontkinter

解决方案


你在这条线上犯了一个非常常见的错误:

button_frame = tk.Frame(self,bg="lightblue").grid(row=0, column=0, sticky="ns")

因为你在做的tk.Frame(...).grid(...)button_frame是被设定的结果.grid(...)。这使得button_frame None. 当您这样做btn_open = ttk.Button(button_frame, ...)时,会将按钮作为根窗口的子级而不是button_frame. 您正在pack根窗口中使用,因此您不能grid用于根窗口中的任何其他小部件。

解决方案是通过将小部件创建与小部件布局分开来正确创建button_frame

button_frame = tk.Frame(...)
button_frame.grid(...)

推荐阅读