简体   繁体   English

如何通过 arguments 并从 tkinter 按钮按下 function 获取返回值?

[英]How do I pass arguments and get return values from a tkinter button press function?

I am trying to get the word entered in the field(exp_textbox) to be passed as an arguement to the function(definition).我试图让在字段(exp_textbox)中输入的单词作为参数传递给函数(定义)。 This is the code below for the dictionary app.这是字典应用程序的以下代码。 the json file is a python dictionary of words and their meaning. json 文件是 python 字典的单词及其含义。 Is there a way i could do this?有没有办法我可以做到这一点?

import tkinter
import json
import difflib

data = json.load(open('data.json'))

main_window = tkinter.Tk()


def definition(w):
    w = w.lower()
    match_word = difflib.get_close_matches(w, data.keys())
    if w in data:
        result = data[w]
        return result
    elif len(match_word) > 0:
        answer = input(f"Did you mean '{match_word[0]}'? [Y/N]").upper()
        if answer == 'Y':
            result = data[match_word[0]]
            return result
        elif answer == 'N':
            return f"'{w}' is not in my dictionary"
        else:
            return 'command not understood'
    else:
        return f"'{w}' is not in my dictionary"


# Window Title
main_window.title('Dictionary')
main_window.geometry('320x320')

# Expression Frame
exp_frame = tkinter.Frame(main_window)
exp_frame.grid(row=0, column=0, columnspan=3, sticky='new')
exp_label = tkinter.Label(exp_frame, text='Enter your Word Here: ')
exp_label.grid(row=0, column=0)

The word provided in the field below should be passed to the function(definition) as an arguement.下面字段中提供的单词应作为参数传递给函数(定义)。

exp_textbox = tkinter.Entry(master=exp_frame)
exp_textbox.grid(row=0, column=1)
exp_label = tkinter.Button(exp_frame, text='Search', command=definition(exp_textbox.get()))
exp_label.grid(row=0, column=3)

# Definition Frame
def_frame = tkinter.Frame(main_window)
def_frame.grid(row=2, column=0, columnspan=3, sticky='new')
def_label = tkinter.Label(def_frame, text='Definition is : ')
def_label.grid(row=2, column=0)

The return value of the function would then be returned here for the user. function 的返回值将在此处返回给用户。

def_textbox = tkinter.StringVar()
tkinter.Entry(def_frame, textvariable=def_textbox).grid(row=2, column=1)
main_window.mainloop()

So there are a couple of problems evident.所以有几个问题很明显。

Running the function on button press按下按钮运行 function

The first issue is that when you are creating the button you immediately run the definition function, instead of only running it when the button is pressed.第一个问题是,当您创建按钮时,您会立即运行定义 function,而不是仅在按下按钮时运行它。 How to pass arguments to a Button command in Tkinter? 如何将 arguments 传递给 Tkinter 中的 Button 命令? has some solutions to this issue, but a simple addition would be:对这个问题有一些解决方案,但一个简单的补充是:

exp_label = tkinter.Button(exp_frame, text='Search', command=lambda: definition(exp_textbox.get()))

Getting the result of running the function得到运行function的结果

The next issue is that you are returning the result of checking the definition to the point of call (to the Button object) which does not know what to do with this.下一个问题是您将检查定义的结果返回到不知道如何处理的调用点(到 Button 对象)。 A quick way to work around this would be to set the value in the textbox directly in the function itself:解决此问题的一种快速方法是直接在 function 本身中设置文本框中的值:

  1. Move the definition of your def_textbox Stringvar above the buttondef_textbox Stringvar 的定义移到按钮上方
  2. Pass the def_textbox to your button commanddef_textbox传递给您的按钮命令
  3. Instead of returning the result, use set to change the textbox不返回结果,而是使用 set 更改文本框

Giving final code like:给出最终代码,如:

import tkinter
import json
import difflib

data = json.load(open('data.json'))

main_window = tkinter.Tk()

def definition(w, txtbox):
    print(f'In definition with {w}')
    w = w.lower()
    match_word = difflib.get_close_matches(w, data.keys())
    if w in data:
        result = data[w]
        txtbox.set(result)
    elif len(match_word) > 0:
        answer = input(f"Did you mean '{match_word[0]}'? [Y/N]").upper()
        if answer == 'Y':
            result = data[match_word[0]]
            txtbox.set(result)
        elif answer == 'N':
            txtbox.set(f"'{w}' is not in my dictionary")
        else:
            txtbox.set('command not understood')
    else:
        txtbox.set(f"'{w}' is not in my dictionary")


# Window Title
main_window.title('Dictionary')
main_window.geometry('320x320')

def_textbox = tkinter.StringVar()
# Expression Frame
exp_frame = tkinter.Frame(main_window)
exp_frame.grid(row=0, column=0, columnspan=3, sticky='new')
exp_label = tkinter.Label(exp_frame, text='Enter your Word Here: ')
exp_label.grid(row=0, column=0)

exp_textbox = tkinter.Entry(master=exp_frame)
exp_textbox.grid(row=0, column=1)
exp_label = tkinter.Button(exp_frame, text='Search', command=lambda: definition(exp_textbox.get(), def_textbox))
exp_label.grid(row=0, column=3)

# Definition Frame
def_frame = tkinter.Frame(main_window)
def_frame.grid(row=2, column=0, columnspan=3, sticky='new')
def_label = tkinter.Label(def_frame, text='Definition is : ')
def_label.grid(row=2, column=0)

tkinter.Entry(def_frame, textvariable=def_textbox).grid(row=2, column=1)
main_window.mainloop()

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

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