【发布时间】:2018-05-19 21:43:26
【问题描述】:
美好的一天。学习 tkinter,使用 Python 3.5。首先,我在 root() 中使用 pack() 处理帧,然后在打包帧内使用 grid() 取得了一些成功。然后我决定使用网格将框架放置在这些框架内的根和网格中。基本上从用户那里获取一些信息 - 用信息做一些事情 - 一些触发器来重置帧 - 重新开始。在 pack() 中的 destroy() 过程中,框架似乎被附加到根目录,而在 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()
【问题讨论】: