Python 特金特。根目录的子帧不显示

Python 特金特。根目录的子帧不显示,python,tkinter,Python,Tkinter,这是我的密码: import tkinter as tk import sys class MapCreator: def __init__(self, root): #initialize the root window self.root = root self.root.geometry('800x600+0+0') self.root.resizable(width = False, height = False)

这是我的密码:

import tkinter as tk
import sys

class MapCreator:
    def __init__(self, root):
        #initialize the root window
        self.root = root
        self.root.geometry('800x600+0+0')
        self.root.resizable(width = False, height = False)
        self.menubar = None
        self.mapFrame = tk.Frame(self.root,bd = 2,bg = 'white')
        self.mapFrame.grid(column = 3, row = 3)
        #self.mapFrame.pack()
        self.__create_menubar()
    def __create_menubar(self):
        #Create the menubar
        self.menubar = tk.Menu()
        #config , which used to change the options
        self.root.configure(menu = self.menubar)

        #create a File menu and add it to the menubar
        #add a new menu
        file_menu = tk.Menu(self.menubar, tearoff=False)
        #File cascade
        self.menubar.add_cascade(label="File", menu=file_menu)
        #New
        file_menu.add_command(label="New", command=self.__create_new_map)
        #Open
        file_menu.add_command(label="Open", command=self.__open_new_map)
        #Svae
        file_menu.add_command(label="Save", command=self.__save_cur_map)
        #Save as
        file_menu.add_command(label="Save as", command=self.__saveas_cur_map)

    def __create_new_map(self):
        #if not saved , tell the editor
        pass

    def __open_new_map(self):
        #if not saved , tell the editor
        pass

    def __save_cur_map(self):
        #first time save or not
        pass

    def __saveas_cur_map(self):
        pass


    pass
if __name__ == "__main__":
    root = tk.Tk()
    app = MapCreator(root)
    root.mainloop()
我想要一个子帧来显示其他内容。但我找不到我的子帧

顺便说一下,我不熟悉网格方法

有人能告诉我为什么不能显示子帧吗?

您的帧正在显示。然而,因为它没有任何东西在里面,它的自然大小只有一个像素宽和一个像素高,使它几乎看不见

把某物放在框架中,或者给它一个明确的宽度和高度

您的另一个选项是配置网格,使框架粘在其所在单元的侧面,单元展开以填充窗口中的任何额外空间

例如:

self.mapFrame.grid(column = 3, row = 3, sticky="nsew")
root.grid_rowconfigure(3, weight=1)
root.grid_columnconfigure(3, weight=1)
在tkinter中,大多数小部件和框架在未指定时采用默认大小。您可以显式地指定大小,给它一个高度和宽度,或者让框架或小部件拉伸到尺寸

下面是对tkinters网格和大小的简要介绍。这是我喜欢的,而不是指定身高和体重

假设您正在将父框架或任何类型的容器拆分为网格。子对象的“栅格”属性允许您将子对象定位到父对象栅格的特定部分。例如,您希望您的子对象位于父对象的0,0中。然后你说。child.gridrow=0,column=0

注意:您没有指定栅格的宽度和高度。它将自身大小重新调整为子窗口小部件的默认大小。按钮具有特定的大小。如果子帧是另一个帧,且该帧为空。它将不可见,因为大小为1px宽和1px高

rowspan,columnspan允许您调整子小部件占用的行数或列数。这与在Excel表格中合并单元格的概念类似。因此,当您说child.gridrow=0、column=0、rowspan=2、columnspan=2时,您指定小部件放置在0,0中,占据2行2列

既然你有了它,你如何让它伸展?这是使用粘滞和权重完成的。对于子窗口小部件,您可以指定粘性。通过这个,您可以定义要拉伸的方向。child.gridrow=0,column=0,sticky='ew'指定小部件可以水平拉伸。gridrow=0,column=0,粘性“nsew”指定完全拉伸。指定粘性并不会拉伸小部件,它只是指示可以拉伸的方向。可以指定父栅格的列和行的权重,以指定不同的拉伸级别。parent.grid\u columnconfigure0,weight=1将权重1添加到列0。所以列0被拉伸。如果要拉伸多个列,则“权重”指定这些不同列之间的增加比率。parent.grid_rowconfigure0,weight=1对行执行相同的操作

考虑到所有这些背景。如果您将grid_rowconfigure和grid_columnconfigure添加到根目录中,并将粘性添加到mapFrame中。你得到这个:

def __init__(self, root):
    #initialize the root window
    self.root = root
    self.root.geometry('800x600+0+0')
    self.root.resizable(width = False, height = False)
    self.menubar = None
    self.mapFrame = tk.Frame(self.root,bd = 2,bg = 'white')
    self.mapFrame.grid(row = 0, sticky='nsew')
    self.root.grid_columnconfigure(0, weight=1)
    self.root.grid_rowconfigure(0, weight=1)
    #self.mapFrame.pack()
    self.__create_menubar()
这应该是可行的:


可以在child.grid语句中指定许多其他属性。我通常使用的另一个是padx-pady,它是x,y方向的填充,而ipadx-ipady是内部填充。有关更多详细信息,请参阅

如果您不熟悉网格,为什么要使用它,以及为什么没有阅读有关如何使用它的文档?