[英]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
至少指定row
和column
选项,以便永远不会混淆将小部件放置在何处。
我也遇到了条目是type(')而不是tkinter.Entry的问题
这个问题没有任何意义。 Entry
创建的对象将始终为<class 'tkinter.Entry'>
,并且get
方法在该类型的对象上使用时将始终有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.