2011-10-11 83 views

回答

1

除非我在問題描述中遺漏了某些內容,否則「確定」按鈕命令只需將「輸入」字段中的值複製到「列表框」。除此之外,還有更多嗎?

from tkinter import Tk, Frame, Label, Entry, Button, Listbox 

def ok_button(): 
    li.delete(0, "end") 
    for i in range(len(fields)): 
     li.insert("end", e[i].get()) 

root = Tk() 
root.title("Listbox") 

cf = Frame(root) 
cf.pack() 
fields = ("Name", "Address", "Phone", "DOB") 
e = [] 
for f in fields: 
    i = len(e) 
    Label(cf, text=f).grid(column=2, row=i, sticky="e") 
    e.append(Entry(cf, width=16)) 
    e[i].grid(column=4, row=i) 
Button(cf, text="OK", command=ok_button).grid(column=2, row=10, columnspan=3) 
li = Listbox(cf) 
li.grid(column=2, row=8, columnspan=3) 

root.mainloop() 
相關問題