2015-10-14 41 views
-1

我正在創建一個python代碼來打開計算機默認瀏覽器中的網站。同時呼籲e1_var.get我得到這個異常:「entry.get()異常在Tkinter回調」時調用entry_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() 

回答

2

請閱讀並按照good code examples採取行動。在此代碼中,e1_var=StringVar必須更改爲e1_var=StringVar()以將e1_var設置爲實例而不是類。你的bug使得e1_var.get成爲一種無約束的方法(在2.7中),因此是錯誤信息。