tkinter:框架和网格

时间:2018-05-18 16:16:00

标签: python tkinter tk

对于我的生活,我无法理解如何使用grid()来管理Frame Python 3.6 )中的小部件。下面的代码试图在根窗口中显示2乘2的ListBox es矩阵。

    import tkinter as TK

    root = TK.Tk()
    root.title('My App')
    rootWidth = 768
    rootHeight = 768
    root.geometry('{}x{}+0+0'.format(rootWidth, rootHeight))
    root.resizable(width=False, height=False)

    frame00 = TK.Frame(root, bd=2, relief=TK.SUNKEN)
    box00 = TK.Listbox(frame00, bd=0)
    box10 = TK.Listbox(frame00, bd=0)

    box00.grid(row=0, sticky=TK.N)
    box10.grid(row=1, sticky=TK.S)
    frame00.grid(row=0, column=0, sticky=TK.W)
    frame00.rowconfigure(0, weight=1)
    frame00.rowconfigure(1, weight=1)


    frame01 = TK.Frame(root, bd=2, relief=TK.SUNKEN)
    box01 = TK.Listbox(frame01, bd=0)
    box11 = TK.Listbox(frame01, bd=0)

    box01.grid(row=0, sticky=TK.N)
    box11.grid(row=1, sticky=TK.S)
    frame01.grid(row=0, column=1, sticky=TK.E)
    frame01.rowconfigure(0, weight=1)
    frame01.rowconfigure(1, weight=2)

    root.grid_rowconfigure(0, weight=1)
    root.grid_columnconfigure(0, weight=1)
    root.grid_columnconfigure(1, weight=1)

    for i in range(20):
        box00.insert(TK.END, 'test')
        box10.insert(TK.END, 'test')
        box01.insert(TK.END, 'test')
        box11.insert(TK.END, 'test')

最后,我只在GUI中看到两个ListBoxes(即只有一行)而不是其中的四个。 但是,如果我每Frame使用一个ListBox,那么一切正常。

    import tkinter as TK

    root = TK.Tk()
    root.title('My App')
    rootWidth = 768
    rootHeight = 768
    root.geometry('{}x{}+0+0'.format(rootWidth, rootHeight))
    root.resizable(width=False, height=False)

    frame00 = TK.Frame(root, bd=2, relief=TK.SUNKEN)
    box00 = TK.Listbox(frame00, bd=0)
    frame10 = TK.Frame(root, bd=2, relief=TK.RAISED)
    box10 = TK.Listbox(frame10, bd=0)

    box00.grid(row=0, sticky=TK.N)
    box10.grid(row=0, sticky=TK.S)
    frame00.grid(row=0, column=0, sticky=TK.W)
    frame10.grid(row=1, column=0, sticky=TK.W)


    frame01 = TK.Frame(root, bd=2, relief=TK.SUNKEN)
    box01 = TK.Listbox(frame01, bd=0)
    frame11 = TK.Frame(root, bd=2, relief=TK.RAISED)
    box11 = TK.Listbox(frame11, bd=0)

    box01.grid(row=0, sticky=TK.N)
    box11.grid(row=0, sticky=TK.S)
    frame01.grid(row=0, column=1, sticky=TK.E)
    frame11.grid(row=1, column=1, sticky=TK.E)

    root.grid_rowconfigure(0, weight=1)
    root.grid_columnconfigure(0, weight=1)
    root.grid_columnconfigure(1, weight=1)

    for i in range(20):
        box00.insert(TK.END, 'test')
        box10.insert(TK.END, 'test')
        box01.insert(TK.END, 'test')
        box11.insert(TK.END, 'test')

是否在Frame内部,您只能使用pack()

更新

线程中的人指出,在原始代码列表中,我没有在column次调用中使用grid()关键字参数。事实上,我做了,只是我在发布之前在最新的尝试中删除了它们,这基本上导致了相同的结果。

以下是包含column参数的新版本,该参数仅显示两个ListBox个。

root = TK.Tk()
root.title('Script Launcher')
rootWidth = 768
rootHeight = 768
root.geometry('{}x{}+0+0'.format(rootWidth, rootHeight))
root.resizable(width=False, height=False)

frame00 = TK.Frame(root, bd=2, relief=TK.SUNKEN)
box00 = TK.Listbox(frame00, bd=0)
box10 = TK.Listbox(frame00, bd=0)

box00.grid(row=0, column=0, sticky=TK.N)
box10.grid(row=1, column=0, sticky=TK.S)
frame00.grid(row=0, column=0, sticky=TK.W)
frame00.rowconfigure(0, weight=1)
frame00.rowconfigure(1, weight=1)
frame00.columnconfigure(0, weight=1)


frame01 = TK.Frame(root, bd=2, relief=TK.SUNKEN)
box01 = TK.Listbox(frame01, bd=0)
box11 = TK.Listbox(frame01, bd=0)

box01.grid(row=0, column=1,  sticky=TK.N)
box11.grid(row=1, column=1, sticky=TK.S)
frame01.grid(row=0, column=1, sticky=TK.E)
frame01.rowconfigure(0, weight=1)
frame01.rowconfigure(1, weight=1)
frame01.columnconfigure(0, weight=1)

root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=1)

for name in range(20)
    box00.insert(TK.END, 'test')
    box10.insert(TK.END, 'test')
    box01.insert(TK.END, 'test')
    box11.insert(TK.END, 'test')


root.mainloop()

3 个答案:

答案 0 :(得分:1)

grid()方法告诉GridManager如何处理调用grid()的小部件。所以,如果你调用Tk()。grid(),那将毫无意义。我更喜欢像MyApp这样的类作为基础级别"包装器"对于所有内部小部件。

在我的系统上重写,这会产生以下形式的GUI:

FALSE

外层是----------------------------------- | | | | | | | ListBox | ListBox | | | | | | | ----------------------------------- | | | | | | | ListBox | ListBox | | | | | | | ----------------------------------- ,其第一个内层是TK.Tk()。然后分为左右两个,每个TK.Frame()。看起来像:

TK.Frame()

然后我们通过设置行的权重垂直地对每个内部框架进行网格化。

-----------------------------------
|                |                |
|                |                |
|                |                |
|                |                |
|                |                |
|     Frame      |      Frame     |
|                |                |
|                |                |
|                |                |
|                |                |
|                |                |
-----------------------------------

答案 1 :(得分:0)

您忘记将column添加到grid,请参阅此示例。

box00.grid(row=0, column=45, sticky=TK.N)

grid的工作方式类似于包含rowscolumn

的Excel工作表

检查此link以了解更多相关信息。那里有很好的记录。

完整代码

import tkinter as TK

root = TK.Tk()
root.title('My App')
rootWidth = 768
rootHeight = 768
root.geometry('{}x{}+0+0'.format(rootWidth, rootHeight))
root.resizable(width=False, height=False)

frame00 = TK.Frame(root, bd=2, relief=TK.SUNKEN)
box00 = TK.Listbox(frame00, bd=0)
box10 = TK.Listbox(frame00, bd=0)

box00.grid(row=0, column=75, sticky=TK.N)
box10.grid(row=1, column=5, sticky=TK.S)
frame00.grid(row=0, column=0, sticky=TK.W)
frame00.rowconfigure(0, weight=1)
frame00.rowconfigure(1, weight=1)

frame01 = TK.Frame(root, bd=2, relief=TK.SUNKEN)
box01 = TK.Listbox(frame01, bd=0)
box11 = TK.Listbox(frame01, bd=0)

box01.grid(row=0, column=7, sticky=TK.N)
box11.grid(row=1, column=60,  sticky=TK.S)
frame01.grid(row=0, column=1, sticky=TK.E)
frame01.rowconfigure(0, weight=1)
frame01.rowconfigure(1, weight=2)

root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=1)

for i in range(20):
    box00.insert(TK.END, 'test')
    box10.insert(TK.END, 'test')
    box01.insert(TK.END, 'test')
    box11.insert(TK.END, 'test')

root.mainloop()

答案 2 :(得分:0)

您可以在Frame中使用grid()。这是一个例子。希望它有所帮助...

    import tkinter as tk
    from tkinter import *
    from tkinter import ttk

    class GUI:

        def __init__(self, master):
            mainframe = ttk.Frame(master)
            mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
            mainframe.columnconfigure(0, weight=1)
            mainframe.rowconfigure(0, weight=1)
            list1 = Listbox(master, bd=0)
            list1.grid(column=0, row=0)
            list2 = Listbox(master, bd=0)
            list2.grid(column=1, row=0)
            separator1 = Frame(list1, height=2, bd=1, relief=SUNKEN)
            separator1.grid(column=0, row=0)
            separator2 = Frame(list1, height=2, bd=1, relief=SUNKEN)
            separator2.grid(column=1, row=0)
            separator3 = Frame(list2, height=2, bd=1, relief=SUNKEN)
            separator3.grid(column=2, row=0)
            separator4 = Frame(list2, height=2, bd=1, relief=SUNKEN)
            separator4.grid(column=3, row=0)
            e1 = Label(separator1, text='Label1')
            e1.grid(sticky=W+E)
            e2= Label(separator2, text='Label2')
            e2.grid(sticky=W+E)
            e3 = Label(separator3, text='Label3')
            e3.grid(sticky=W+E)
            e4= Label(separator4, text='Label4')
            e4.grid(sticky=W+E)    

    root = Tk()
    my_gui = GUI(root)
    root.mainloop()

这是tkinter http://effbot.org/tkinterbook/grid.htm

的一个非常好的解释