简体   繁体   English

pack()和grid()tkinter的区别

[英]Differences in pack() and grid() tkinter

Good Day. 美好的一天。 Learning tkinter, using Python 3.5. 使用Python 3.5学习tkinter。 To begin I got some success using pack() for frames in root() then used grid() inside the packed frames. 首先,我在root()的帧中使用pack()获得了一些成功,然后在打包的帧中使用了grid()。 Then I decided to use grid to place Frames in root and grid inside these frames. 然后,我决定使用网格在根目录中放置框架,并在这些框架中放置网格。 Basically get some info from user - do something with info - some trigger to reset the frames - start over. 基本上从用户那里获得一些信息-使用信息做一些事情-一些重置帧的触发器-重新开始。 During the process of destroy() in pack() it appears that Frames are appended to root, and in grid() it appears they are inserted. 在pack()中的destroy()过程中,似乎将框架附加到根,而在grid()中,似乎将它们插入。 Is this correct? 这个对吗? The difference in the code examples is in pack() example I use: 代码示例中的区别在于我使用的pack()示例中:

On another note - I have also run into the problem of the entries being of type( '<class 'tkinter.Entry Object'> ) instead of tkinter.Entry -- I was not able to reproduce this today. 另一个要注意的是-我还遇到了条目类型为'<class 'tkinter.Entry Object'>'<class 'tkinter.Entry Object'> )而不是tkinter.Entry -我今天无法重现。 is there a way to read such a value as get() did not work? 有没有办法读取get()这样的值不起作用?

Thanks 谢谢

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

However in the grid() example I needed to use: 但是在grid()示例中,我需要使用:

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

Min working example using pack(): 使用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()  

example using grid(): 使用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()

During the process of destroy() in pack() it appears that Frames are appended to root, and in grid() it appears they are inserted. 在pack()中的destroy()过程中,似乎将框架附加到根,而在grid()中,似乎将它们插入。

Not exactly. 不完全是。 In both cases they are added to the frame according to their options. 在这两种情况下,它们都将根据其选项添加到框架中。 In the case of pack , the default placement option (eg: side ) is to place the widget at the top of the available space in the parent/master. pack的情况下,默认放置选项(例如: side )是将小部件放置在父/母版中可用空间的顶部。

In the case of grid the default is to use the first unoccupied row after all other widgets in the same parent/master. grid的情况下,默认设置是在同一父/母版中所有其他小部件之后使用第一个未占用的行。 If not specified, the column number will be zero. 如果未指定,则列号将为零。

Best practices dictate that you should always specify at least the side option with pack , and the row and column option with grid so that there is never any confusion about where the widget is intended to be placed. 最佳实践表明,您应该始终至少使用pack指定side选项,并使用grid至少指定rowcolumn选项,以便永远不会混淆将小部件放置在何处。

I have also run into the problem of the entries being of type(') instead of tkinter.Entry 我也遇到了条目是type(')而不是tkinter.Entry的问题

That question doesn't make any sense. 这个问题没有任何意义。 The objects created by Entry will always be of type <class 'tkinter.Entry'> , and the method get will always work when used on an object of that type. Entry创建的对象将始终为<class 'tkinter.Entry'> ,并且get方法在该类型的对象上使用时将始终有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM