特金特.根子帧不显示 [英] Tkinter. subframe of root does not show

查看:25
本文介绍了特金特.根子帧不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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()

我想要一个子框架来显示其他东西.但是我找不到我的子框架.

I want to have a subframe to show other things. But I cannot find my subframe.

顺便说一下,我不熟悉 grid() 方法.

By the way, I'm not familiar with the grid() method.

那么谁能告诉我为什么我不能显示子帧的原因?

So can someone tell me the reason why I cannot make the subframe show?

推荐答案

tkinter 中,大多数小部件和框架在未指定时采用默认大小.您可以明确指定尺寸(为其指定高度和宽度)或让框架或小部件拉伸到尺寸.

In tkinter most of the widgets and frames take the default size when not specified. You can specify the size explicitly (give it a height and width) or have the frame or widgets stretch to dimensions.

这里简单介绍一下 tkinter 的网格和大小.这是我更喜欢的,而不是指定身高和体重.

Here is a brief introduction to tkinters grid and size. This is what I prefer, rather than specifying the height and weight.

想象一下,您正在将父框架(或任何类型的容器)拆分为一个网格.child 的 grid 属性允许您将您的孩子定位到 Parent 网格的特定部分.例如,您希望您的孩子在父母的 (0,0) 中.那你说.child.grid(row=0, column = 0).

Imagine you are splitting the Parent Frame (or any kind of container) into a grid. The grid attribute of the child allows you to position your child into the specific part of the Parent's grid. Like for example, you want your child to be in (0,0) of the parent. then you say. child.grid(row=0, column = 0).

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

Note: You are not specifying the width and height of the grid. It re-sizes itself to the default size of the child widget. Button has a specific size. If the child is another frame and that frame is empty. It will not be visible because the size is 1px wide and 1px height.

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

rowspan, columnspan allows you to adjust how many rows or columns the child widget occupies. This is a similar concept to merging cells in Excel table. So when you say child.grid(row=0, column = 0, rowspan=2, columnspan=2) You are specifying that the widget is placed in (0,0) and occupies 2 rows and 2 columns.

既然你有了它,你如何让它伸展?这是使用粘性和权重完成的.对于子小部件,您指定粘性.有了这个,您就可以定义要拉伸的方向.child.grid(row=0, column = 0,sticky='ew') 指定小部件可以水平拉伸.child.grid(row=0, column = 0,sticky 'nsew') 指定完全拉伸.指定粘性不会拉伸小部件,它只是指示可以拉伸的方向.您可以指定父网格的列和行的权重以指定不同的拉伸级别.parent.grid_columnconfigure(0, weight=1) 将权重 1 添加到第 0 列.因此第 0 列被拉伸.如果您有多个要拉伸的列,则权重指定这些不同列之间的增加比率.parent.grid_rowconfigure(0, weight=1) 对行做同样的事情.

Now that you have that, how do you make it stretch? This is done using stickys and weights. For the child widget, you specify the sticky. With this you are defining which directions you would like to stretch. child.grid(row=0, column = 0, sticky='ew') specifies that the widget can stretch horizontally. child.grid(row=0, column = 0, sticky 'nsew') specifies full stretch. Specifying the sticky does not stretch the widget, it just dictates what direction is can stretch. You specify the weight of the column and row of the parent grid to specify different levels of stretch. parent.grid_columnconfigure(0, weight=1) adds a weight of 1 to column 0. So column 0 is stretched. If you have multiple columns you want to stretch, then weight specifies the ratio of increase between those different columns. parent.grid_rowconfigure(0, weight=1) does the same for rows.

鉴于所有这些背景.如果您将 grid_rowconfigure 和 grid_columnconfigure 添加到您的根目录并将粘性添加到您的 mapFrame.你得到这个:

Given all that background. If you add the grid_rowconfigure and grid_columnconfigure to your root and add the sticky to your mapFrame. You get this:

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 填充是内部填充.请参阅 http://effbot.org/tkinterbook/grid.htm 了解更多详情

There are lots of other properties you can specify in your child.grid() statement. The other one I usually use are padx pady which is padding in the x,y direction and ipadx ipady which is internal padding. Refer to http://effbot.org/tkinterbook/grid.htm for more details

这篇关于特金特.根子帧不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆