"entry.get() Exception in Tkinter callback" 调用 entry_var.get() 时

"entry.get() Exception in Tkinter callback" when calling entry_var.get()

我正在创建 python 代码以在计算机默认浏览器中打开网站。调用 e1_var.get 时出现异常:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1536, in __call__
    return self.func(*args)
  File "Desktop/Raspberry Pi 2 stuff/Python Scripts/SearchPy.py", line 5, in search
    e1_var.get()
TypeError: unbound method get() must be called with StringVar instance as first argument (got nothing instead)

这个错误对我来说没有意义,因为我已经在 .get() 之前调用了 e1_var 作为我的 StringVar 实例。这是错误的代码:

from Tkinter import *

def search():
    #FAULTY BIT HERE!!
    e1_var.get()
    #and just to test...
    print e1_var
root=Tk()
root.wm_title("SearchPy")
w = 500 # width for the Tk root
h = 500 # height for the Tk root

# get screen width and height
ws = root.winfo_screenwidth() # width of the screen
hs = root.winfo_screenheight() # height of the screen

# calculate x and y coordinates for the Tk root window
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)

# set the dimensions of the screen
# and where it is placed
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
#main buttons and entrys to get url.

e1_var=StringVar #e1_var has been set a StringVar which is a StringVar instance?

l1=Label(root, text="Search").grid(column=0, row=1) #no explanation needed!!
#text entry
e1=Entry(root, textvariable=e1_var).grid(column=1, row=1)
#search button
b1=Button(root, text="Go!", command=search()).grid(row=2)

root.mainloop()

请阅读 good code examples 并采取行动。在此代码中,必须将 e1_var=StringVar 更改为 e1_var=StringVar() 以将 e1_var 设置为实例而不是 class。您的错误使 e1_var.get 成为未绑定的方法而不是绑定的方法(在 2.7 中),因此出现错误消息。