pack()和grid()tkinter的差异

时间:2018-05-19 21:43:27

标签: python tkinter

美好的一天。学习tkinter,使用Python 3.5。首先,我使用pack()获取了一些成功的root()中的帧然后在打包帧内使用grid()。然后我决定使用网格将帧放在这些帧内的根和网格中。基本上从用户那里获得一些信息 - 用信息做一些事情 - 一些触发器重置帧 - 重新开始。在pack()中的destroy()过程中,看起来Frames被附加到root,并且在grid()中它们会被插入。它是否正确?代码示例的不同之处在于我使用的pack()示例:

另一方面 - 我也遇到了类型('<class 'tkinter.Entry Object'>)而不是tkinter.Entry的条目问题 - 我今天无法重现这个问题。有没有办法读取这样的值,因为get()不起作用?

由于

i = root.slaves()
for child in i[1].winfo_children():

但是在grid()示例中,我需要使用:

i = root.grid_slaves()
for child in i[0].winfo_children():

使用pack()的最小工作示例:

def new():
    showEntries()
    D_setup()
    all_entries['D'] = D

def D_setup():
    dx = len(D)
    # D Label
    labD = Label(frame_for_D, text='Place to Enter Value')
    labD.grid(row = dx * 2, column = 0)
    # D Entry
    entD = Entry(frame_for_D, width=50)
    entD.grid(row = dx * 2 + 1, column=0)
    entD.delete(0,END)
    entD.insert(END, s[dx])
    # Append the last Entry
    D.append(entD)
    return

def showEntries():

    if len(D) == 0:
        return



    for number, ent in all_entries.items():
        if str(type(ent)) == "<class 'tkinter.Entry'>":
            print(type(ent))
            print (number, ent.get())
        elif str(type(ent)) == "<class 'list'>":
            print (number, [x.get() for x in ent])
    if len(D) > 5:
        i = root.slaves()
        for child in i[1].winfo_children():
            child.destroy()
        D.clear()
        all_entries.clear()
        D.clear()
        print(all_entries)
        print(D)
        raise SystemExit
    return


#------------------------------------

all_entries = {}
D = []
root = Tk()
root.configure(background = 'green')
# -- Define all frames
frame_for_buttons = Frame(root,background='black')
frame_for_D = Frame(root,background='red')
# -- pack all frames
frame_for_buttons.pack()
frame_for_D.pack()
# -- root
newTButton = Button(frame_for_buttons, text='New\n\n' +
                                             'Enter',
                    activebackground='red',
                    activeforeground='black',
                    borderwidth=10, foreground='black',
                    background='yellow',
                    highlightthickness=20,padx=5,pady=5,
                    command=new)
newTButton.grid(row=0, column=0)

root.mainloop()  

使用grid()的例子:

def new():

    showEntries()
    D_setup()
    all_entries['D'] = D

def D_setup():
    dx = len(D)
    # D Label
    labD = Label(frame_for_D, text='Place to Enter Value')
    labD.grid(row = dx * 2, column = dx)
    # D Entry
    entD = Entry(frame_for_D, width=50)
    entD.grid(row = dx * 2 + 1, column=dx)
    entD.delete(0,END)
    entD.insert(END, s[dx])
    # Append the Entry
    D.append(entD)
    return

def showEntries():

    if len(D) == 0:
        return

    for number, ent in all_entries.items():
        if str(type(ent)) == "<class 'tkinter.Entry'>":
            print(type(ent))
            print (number, ent.get())
        elif str(type(ent)) == "<class 'list'>":
            print([type(x) for x in ent])
            print (number, [x.get() for x in ent])
    if len(D) > 5:
        #i = frame_for_D.grid_slaves()      .winfo_children()
        i = root.grid_slaves()
        for child in i[0].winfo_children():
            child.destroy()
        D.clear()
        all_entries.clear()
        D.clear()
        print(all_entries)
        print(D)
        raise SystemExit
    return


#------------------------------------

all_entries = {}
D = []
root = Tk()
root.configure(background = 'green')
# -- Define all frames
frame_for_buttons = Frame(root,background='black')
frame_for_D = Frame(root,background='red')
# -- Grid all frames
frame_for_buttons.grid(row = 0, column = 0)
frame_for_D.grid(row = 1, column = 0, pady = 10, padx = 10)
# root
newTButton = Button(frame_for_buttons, text='New\n\n' +
                                             'Enter',
                    activebackground='red',
                    activeforeground='black',
                    borderwidth=10, foreground='black',
                    background='yellow',
                    highlightthickness=20,padx=5,pady=5,
                    command=new)
newTButton.grid(row=0, column=0)

root.mainloop()

1 个答案:

答案 0 :(得分:2)

  

在pack()中的destroy()过程中,似乎Frames被附加到root,并且在grid()中看起来它们被插入。

不完全是。在这两种情况下,它们都会根据其选项添加到框架中。在pack的情况下,默认的放置选项(例如:side)是将窗口小部件放在父/主的可用空间的顶部。

grid的情况下,默认是在同一父/主中的所有其他小部件之后使用第一个未占用的行。如果未指定,则列号将为零。

最佳做法要求您应始终至少使用side指定pack选项,并使用row指定columngrid选项,以便永远不会混淆小工具的放置位置。

  

我也遇到了类型(&#39;)而不是tkinter.Entry

的条目问题

这个问题没有任何意义。 Entry创建的对象始终为<class 'tkinter.Entry'>类型,方法get在该类型的对象上使用时始终有效。