Python 2.7 &引用;Tkinter回调中的entry.get()异常;调用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)

Python 2.7 &引用;Tkinter回调中的entry.get()异常;调用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),python-2.7,tkinter,tk,Python 2.7,Tkinter,Tk,这个错误对我来说没有意义,因为我在.get()之前已经调用了e1\u 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

这个错误对我来说没有意义,因为我在
.get()
之前已经调用了
e1\u 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()
请阅读并采取行动。在此代码中,
e1\u var=StringVar
必须更改为
e1\u var=StringVar()
才能将
e1\u var
设置为实例而不是类。您的bug使
e1_var.get
成为未绑定而非绑定的方法(在2.7中),因此会出现错误消息