【问题标题】:get not working for multiple entry widget in tkinter无法在 tkinter 中使用多个条目小部件
【发布时间】:2015-03-30 07:12:07
【问题描述】:

我需要从条目中获取 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)

这里有什么错误。以及如何从条目小部件中获取值

【问题讨论】:

  • 这里有什么错误?你告诉我。你想做什么,它在做什么,你期望什么?
  • 我无法从条目小部件中获取价值。这需要作为争论的另一个功能!但条目[] 为空。 @jedwards
  • 我的意思是,这有很多问题,但是您可以通过将entries = [] 移到函数之外来绕过。
  • 试过了还是不行@jedwards
  • 您在创建entry 之后立即将entry.get() 附加到entries,因此此时entry.get() 返回一个空字符串,因为用户没有在@ 中输入任何内容987654327@ 还没有。因此,entries 是一个空字符串列表。

标签: python get tkinter


【解决方案1】:

正如我在 cmets 中告诉你的,你在创建 entry 之后立即将 entry.get() 附加到 entries,所以此时,entry.get() 返回一个空字符串,因为用户没有输入entry 中的任何内容。因此,entries 是一个空字符串列表。

您需要保存对 Entry 小部件的引用(可能在列表中),然后为搜索按钮创建一个回调函数并等待调用 get() 直到在该回调函数中,如下面的代码 sn-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()

【讨论】:

  • 谢谢。你的脚本工作得很好。但是当我集成到我的脚本中时它不起作用!
  • “不起作用”没有提供足够的详细信息,我无法为您提供帮助。
  • 我已经编辑了这个问题。你现在可以检查吗? @fhdrsdg
  • 您遇到的错误是什么?我猜这是关于entries 没有在searching 中定义,但如果您可以发布错误会有所帮助。
  • 好的,首先command=searching()应该是command=searching
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-03
  • 1970-01-01
  • 2021-07-03
  • 2016-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多