pythontkinter通过函数接受用户输入

2024-04-20 10:56:04 发布

您现在位置:Python中文网/ 问答频道 /正文

1)我收到myText_Box的属性错误

2)我的目标是通过文本框接受用户输入,然后通过API定义方法定义单词。你知道吗

3)我将利用这个post来请求一个字符串——我解决属性错误有多冷?在这个post之后,我试图沿着calc.myText_Box的思路做一些事情

calc = tk.Tk()
calc.title("VocabU")

Question_1 = str("Define which word?")
FRONT_PAGE = ['Define me!', Question_1]

def retrieve_input():
    input = calc.myText_Box.get("1.0",'end-1c')
    define_me = dictionary.get_definition(input)
    return define_me

USER_INP = retrieve_input()

#RESPONSE = str(dictionary.get_definition(input))
# set up GUI
row = 1
col = 0
for i in FRONT_PAGE:
    button_style = 'raised'
    #action =
    action = lambda x = retrieve_input(): click_event(x)
    tk.Button(calc, text = i, width = 17, height = 3, relief = button_style, command = action).grid(row = row, column = col, sticky = 'nesw')
    col += 1
    if col > 0: # if col > 4
        col = 0
        row += 1

display = tk.Entry(calc, width = 40, bg = "white", text = Question_1)
#display.pack
display.grid(row = 2, column = 0, columnspan = 1) # columnspan = 5

Tags: boxinputget属性错误displaycalcaction
2条回答

input是Python中的保留字,请尝试使用另一个!你知道吗

您尚未在https://github.com/phillipsk/dictionary_Merriam-Webster_API的代码中的任何地方定义myText_Box。你知道吗

尝试引用它会引发属性错误,尝试访问对象上任何未定义的属性也会引发属性错误:

>>> a = object()
>>> a.myText_Box
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'myText_Box'

您需要创建文本小部件并将其分配给Tk()实例calc

calc.myText_Box = Text(...)

相关问题 更多 >