【问题标题】:Get the entry with Tkinter [duplicate]使用 Tkinter 获取条目 [重复]
【发布时间】:2018-03-21 11:17:44
【问题描述】:

你好,我有这个代码:

import Tkinter as tk

root = tk.Tk()
margin = 0.23
entry = tk.entry(root).grid(row=1,column=1)

def profit_calculator():
    profit = margin*int(entry.get())
    print(profit)

button_calc = tk.Button(root, text="Calculate", command=profit_calculator).grid(row=2,column=1)

root.mainloop()

我试图执行这段代码,我在条目中输入了一个值,但是当我点击按钮时,我得到了这个:

AttributeError : 'NoneType' object has no attribute 'get'

我必须绝对使用网格,但我不知道为什么会出现这个错误...

你能帮帮我吗?

谢谢你:)

【问题讨论】:

  • 上面的代码不会引发您声称的错误。它引发了AttributeError: 'module' object has no attribute 'entry'。可以通过将条目的初始字符大写来修复:Entry(...。

标签: python python-3.x python-2.7 tkinter


【解决方案1】:

entry 现在是grid 的返回,它始终是None。将显示调用与小部件对象启动分开。替换:

entry = tk.entry(root).grid(row=1,column=1)

与:

entry = tk.Entry(root) # no predefined class named entry
entry.grid(row=1,column=1)

【讨论】:

    猜你喜欢
    • 2015-01-01
    • 2021-05-06
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 2012-05-29
    相关资源
    最近更新 更多