如何在tkinter中使用.get()?

时间:2018-07-12 12:50:47

标签: python tkinter

from tkinter import *

main = Tk()

e = Entry(main).pack()

main.configure(bg = "Black")

s = e.get()

main.mainloop()

但随后出现此错误:

Traceback (most recent call last):
  File "//agsb2/2014intake/kfullman14/My Documents/CICT/Programming U6/Python/Python Challenges/Tkinter test.py", line 9, in <module>
    s = e.get()
AttributeError: 'NoneType' object has no attribute 'get'

任何帮助将不胜感激。提前致谢。

1 个答案:

答案 0 :(得分:0)

尝试一下:

from tkinter import *


def button():
    s = e.get()
    label.config(text ='See? There is a difference.You entered %s.' %(s))

main = Tk()

e = Entry(main, bd=5, width=40)

main.configure(bg = "Black")
button = Button(main, text="Do Something", command=button)
label = Label(main, text="I am needed to show the difference.")

e.pack()
label.pack()
button.pack()

main.mainloop()

您确实声明了变量,但是您从未说过如何以及何时使用它。因此,您没有看到任何更改。在上面的示例中,您有一个按钮,可以读取输入并使用它来更改标签。