<p>在您的代码中存在以下错误:</p>
<ul>
<li><p>您必须将<code>StringVar</code>更改为<code>StringVar()</code>,因为您正在创建一个对象。</p></li>
<li><p>必须将<code>self.locationEntry.bind("<Return>", self.onEnter())</code>更改为<code>self.locationEntry.bind("<Return>", self.onEnter)</code>,因为该函数要求您输入回调的名称</p></li>
<li><p>必须将<code>def onEnter(self):</code>更改为<code>def onEnter(self, event):</code>,因为回调函数接收新变量中的事件信息。</p></li>
<li><p>必须将<code>self.place.get(self)</code>更改为<code>self.place.get()</code>,因为<code>get()</code>函数不需要参数。</p></li>
<li><p>必须将<code>self.place</code>更改为<code>place</code>或其他变量,因为它已经存在。</p></li>
</ul>
<p><strong>代码已更正:</strong></p>
<pre><code>import json, requests, re
from tkinter import *
class Window:
def __init__(self):
self.root = Tk()
self.root.geometry("300x100")
self.place = StringVar()
instructions = Label(self.root, text="Enter in city (City, State/Country) or zipcode.")
self.locationEntry = Entry(self.root, textvariable=self.place)
instructions.pack()
self.locationEntry.bind("<Return>", self.onEnter)
self.locationEntry.pack()
self.root.mainloop()
def onEnter(self, event):
place = self.place.get()
print(place)
Window()
</code></pre>