【问题标题】:python tkinter accepting user input through functionpython tkinter 通过函数接受用户输入
【发布时间】:2015-07-28 21:08:05
【问题描述】:

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

【问题讨论】:

  • 发布整个错误。

标签: python python-2.7 tkinter attributes


【解决方案1】:

您尚未在 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'

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

calc.myText_Box = Text(...)

【讨论】:

    【解决方案2】:

    input是Python中的保留字,请尝试使用不同的!

    【讨论】:

    • input 不是 Python 中的保留字。它是一个内置函数。重新分配它的风格很差,但它不是非法的,也不是 OP 问题的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    相关资源
    最近更新 更多