简体   繁体   English

Tkinter按钮不从函数返回值

[英]Tkinter button not returning value from function

I am currently trying to create a small UI for a function that returns some value from a data frame based on the string that has been entered. 我目前正在尝试为函数创建一个小的UI,该函数根据已输入的字符串从数据框中返回一些值。 The string is split and each individual substring is looked up in my data frame using iloc. 字符串被分割,并使用iloc在我的数据框中查找每个子字符串。 The issue is, when calling this function using a button in Tkinter, nothing is being returned. 问题是,使用Tkinter中的按钮调用此函数时,未返回任何内容。 It works fine without Tkinter so I am unsure where the error is occurring. 如果没有Tkinter,它可以正常工作,所以我不确定错误发生在哪里。

master = Tk()
e1 = Entry(master)

list_of_inputs = e1.get().split()

def show_entry_fields():
    i=0
    while (i<len(list_of_inputs)):
        return (backend.loc[backend['Keyword'] == list_of_inputs[i]]) 
        i=i+1

Label(master, text="Enter Title").grid(row=0)

e1.grid(row=0, column=1)

Button(master, text='Show', command=show_entry_fields).grid(row=0, column=2, sticky=W, pady=4)

mainloop( )

Couple of things: 几件事情:

  • You are checking the value of your Entry before the user has a chance to fill it in. You should get the user input only when the button is pressed. 您正在检查输入的值,然后用户才有机会填写。只有在按下按钮时,才应获得用户输入。
  • Your while loop in the show_entry_fields() function will always run once regardless of how many inputs provided, since you put a return statement inside the loop 无论您提供了多少输入, show_entry_fields()函数中的while循环将始终运行一次,因为您在循环中放入了return语句
  • Your handler function has to modify an existing data structure or graphical component, if you return your result you cant collect it 您的处理程序函数必须修改现有的数据结构或图形组件,如果返回结果则无法收集它

Possible implementation: 可能的实现:

master = Tk()
e1 = Entry(master)


def show_entry_fields():
    list_of_inputs = e1.get().split()
    result = []
    for entry in list_of_inputs:
        result.append(backend.loc[backend['Keyword']] == entry)
    # Alternatively, you can create the list with
    # result = [backend.loc[backend['Keyword']] == entry for entry in list_of_inputs]
    # Here, deal with the result    

Label(master, text="Enter Title").grid(row=0)
e1.grid(row=0, column=1)
Button(master, text='Show', command=show_entry_fields).grid(row=0, column=2, sticky=W, pady=4)
mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM