网格和放置小部件:再次

时间:2018-01-11 10:32:19

标签: python tkinter grid widget

逗人,

虽然我想简单一点,但我仍未能创建一类" windows"默认情况下,它将具有基本菜单(不在代码中),10行和10个cls的帧,并且在该帧的最后一个单元格中(row = 9,col = 9)a" Close"按钮。

然后,我可以创建几个将继承自那个的类,并添加更多的小部件,命令,......非常基本

是的但是,虽然我给细胞增加了重量,......,......按钮仍在左上角,而不是右下角。使用.grid()

管理小部件时我错过了什么

很多

import tkinter as tk
from tkinter import ttk

class myWindows(tk.Tk):
    def __init__(self,
                 pWtitle='',
                 pParent = '',
                 pIsOnTop = False,
                 pWidth=800,
                 pHeight=600,
                 pIsResizable=False,
                 pNbRows = 10,
                 pNbCols = 10): 
        tk.Tk.__init__(self)

        tk.Tk.wm_title(self, pWtitle)
        self.geometry('%sx%s' % (pWidth, pHeight))
        if pIsResizable :
            self.minsize(pWidth, pWidth)

        rFrame =tk.Frame(self, borderwidth=1, relief="ridge")


        #to make it simple by default, there will be a grid of 10rows and 10columns
        for r in range(pNbRows) :
            rFrame.grid_rowconfigure(r,weight=1)
        for c in range(pNbCols) :
            rFrame.grid_columnconfigure(c,weight=1)

        rFrame.grid(row=0, column=0, sticky='ns')

        #all windows will have a quit button on the bottom right corner (last cells)
        #Need deduct 1 in the parameter as indexes start from 0

        bt=ttk.Button(self, text='Close',command=self.quit)
        bt.grid(row=pNbRows -1, column=pNbCols -1, sticky='se')


app = myWindows( pWtitle='MAIN')
app.mainloop()

3 个答案:

答案 0 :(得分:0)

您正在rFrame内部配置权重,但是您将rFrame和按钮直接放在根窗口中。您尚未为根窗口中的任何行或列配置权重。

答案 1 :(得分:0)

网格不显示不包含任何内容的行和列。尝试,例如在网格的每一行和每列中添加一个带有空图片的占位符标签(Label =(self,image = PhotoImage())),直到用真实的东西填充它。资源 http://effbot.org/tkinterbook/grid.htm

  

minsize =定义行的最小大小。 请注意,如果有一行   完全为空,即使此选项也不会显示   集。

答案 2 :(得分:0)

最后,我想出了一个解决方案:

import tkinter as tk
from tkinter import ttk


class myWindows(tk.Tk):
def __init__(self,
             pWtitle='',
             pParent = '',
             pIsOnTop = False,
             pWidth=800,
             pHeight=600,
             pIsResizable=False
             ): 
    tk.Tk.__init__(self)

    tk.Tk.wm_title(self, pWtitle)
    self.geometry('%sx%s' % (pWidth, pHeight))
    if pIsResizable :
        self.minsize(pWidth, pHeight)

    #all windows will have a quit button on the bottom right corner (last cells)
    #Need deduct 1 in the parameter as indexes start from 0

    bt=ttk.Button(self, text='Close',command=self.quit)
    bt.grid(row=1, column=0, sticky='se')

    #to make it simple by default, there will be a container on top of button
    rFrame =tk.Frame(self, borderwidth=1, bg = 'blue', relief="ridge")
    rFrame.grid(row=0, column=0)


    #give max weight to the first cell to 
    #make sure the container is filling up the empty space
    #on top of the button(s)
    self.grid_rowconfigure(0, weight = 1)
    self.grid_rowconfigure(1, weight =0)
    self.grid_columnconfigure(0, weight =1)


app = myWindows( pWtitle='MAIN')
app.mainloop()