我需要从条目中获取3个值并将其作为另一个函数的参数传递以搜索输入的内容。但我无法得到这些价值观。这是我的代码。
def read_files():
input=entry1.get()
with open(input, newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for line in spamreader:
contents.append(line)
entries = []
val=["no","num2","date"]
for i in range(3):
label = Label(f2, text=val[i])
label.grid(row=i, column=0)
entry = Entry(f2, width=25)
entry.grid(row=i, column=1)
entries.append(entry.get())
button1 = ttk.Button(f2, text="Search", command=search(**pass the get value here**))
button1.grid(row=4, column=0)
button2 = ttk.Button(f2, text="Quit")
button2.grid(row=4, column=1)
button2.bind ('<ButtonPress>', lambda e: exit())
main = Tk()
main.title("SEARCH")
main.grid()
#main.geometry('640x480')
frame1 = ttk.Frame(main, height=200, width=200)
frame1.grid()
f2 = Frame(main, height=300, width = 300)
f2.grid()
entry1 = Entry(frame1, width=30)
entry1.pack()
button1 = ttk.Button(frame1, text="File", command=read_files)
button1.pack()
#button1.bind ('<ButtonPress>', lambda e: progressbar.start())
button2 = ttk.Button(frame1, text="Quit")
button2.pack()
button2.bind ('<ButtonPress>', lambda e: exit())
frame2 = ttk.Frame(main, height=100, width=100)
frame2.grid()
listbox = Listbox(frame2, height=100, width=100)
listbox.pack(fill=BOTH, expand=YES)
这里的错误是什么。以及如何从条目小部件中获取值
答案 0 :(得分:3)
正如我在评论中告诉您的那样,您在创建entry.get()
后立即将entries
附加到entry
,所以此时entry.get()
会返回空string,因为用户尚未在entry
中输入任何内容。因此,entries
是一个空字符串列表。
您需要保存对Entry小部件的引用(可能在列表中),然后为搜索按钮创建一个回调函数,并等待调用get()
,直到在该回调函数中,如下面的代码片段< / p>
from Tkinter import *
import ttk
def search():
for entry in entries:
print entry.get()
f2 = Tk()
entries = []
val=["no","num2","date"]
for i in range(3):
label = Label(f2, text=val[i])
label.grid(row=i, column=0)
entry = Entry(f2, width=25)
entry.grid(row=i, column=1)
entries.append(entry)
button1 = ttk.Button(f2, text="Search", command=search)
button1.grid(row=4, column=0)
f2.mainloop()