簡體   English   中英

pack()和grid()tkinter的區別

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

美好的一天。 使用Python 3.5學習tkinter。 首先,我在root()的幀中使用pack()獲得了一些成功,然后在打包的幀中使用了grid()。 然后,我決定使用網格在根目錄中放置框架,並在這些框架中放置網格。 基本上從用戶那里獲得一些信息-使用信息做一些事情-一些重置幀的觸發器-重新開始。 在pack()中的destroy()過程中,似乎將框架附加到根,而在grid()中,似乎將它們插入。 這個對嗎? 代碼示例中的區別在於我使用的pack()示例中:

另一個要注意的是-我還遇到了條目類型為'<class 'tkinter.Entry Object'>'<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()

在pack()中的destroy()過程中,似乎將框架附加到根,而在grid()中,似乎將它們插入。

不完全是。 在這兩種情況下,它們都將根據其選項添加到框架中。 pack的情況下,默認放置選項(例如: side )是將小部件放置在父/母版中可用空間的頂部。

grid的情況下,默認設置是在同一父/母版中所有其他小部件之后使用第一個未占用的行。 如果未指定,則列號將為零。

最佳實踐表明,您應該始終至少使用pack指定side選項,並使用grid至少指定rowcolumn選項,以便永遠不會混淆將小部件放置在何處。

我也遇到了條目是type(')而不是tkinter.Entry的問題

這個問題沒有任何意義。 Entry創建的對象將始終為<class 'tkinter.Entry'> ,並且get方法在該類型的對象上使用時將始終有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM