我正在尝试使用Python3.5、Tkinter和weather Underground API创建一个天气应用程序。我试图放入一个Entry
小部件,用户可以在其中输入一个位置。我这样做是为了当用户点击enter时,它将显示输入位置的温度和当前条件。下面是我创建GUI窗口的代码:
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):
self.place = self.place.get(self)
Window()
当我运行程序时,没有窗口出现,我得到以下错误消息:
Traceback (most recent call last): File "C:/Users/jsorh/OneDrive/Documents/School/Web Design/weather/App/WeatherApp.py", line 70, in Window() File "C:/Users/jsorh/OneDrive/Documents/School/Web Design/weather/App/WeatherApp.py", line 20, in init self.locationEntry.bind("", self.onEnter()) File "C:/Users/jsorh/OneDrive/Documents/School/Web Design/weather/App/WeatherApp.py", line 26, in onEnter self.place = self.place.get(self) File "C:\Users\jsorh\AppData\Local\Programs\Python\Python35-32\lib\tkinter__init__.py", line 333, in get value = self._tk.globalgetvar(self._name) AttributeError: 'Window' object has no attribute '_tk'
我在网上找过一些解决办法,但我真的不知道怎么解决这个问题。我是个编程初学者,所以请尽可能简单地解释一下。谢谢。在
此代码可能对您使用full
在您的代码中存在以下错误:
您必须将
StringVar
更改为StringVar()
,因为您正在创建一个对象。必须将
self.locationEntry.bind("<Return>", self.onEnter())
更改为self.locationEntry.bind("<Return>", self.onEnter)
,因为该函数要求您输入回调的名称必须将
def onEnter(self):
更改为def onEnter(self, event):
,因为回调函数接收新变量中的事件信息。必须将
self.place.get(self)
更改为self.place.get()
,因为get()
函数不需要参数。必须将
self.place
更改为place
或其他变量,因为它已经存在。代码已更正:
相关问题 更多 >
编程相关推荐