创建Tkinter GUI时出错:'Window'对象没有'_tk'属性

2024-03-29 07:36:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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'

我在网上找过一些解决办法,但我真的不知道怎么解决这个问题。我是个编程初学者,所以请尽可能简单地解释一下。谢谢。在


Tags: inpyselfgetinitlineplaceroot
2条回答
import json
import codecs
import urllib , cStringIO
import string
from Tkinter import *

weather_api =urllib.urlopen('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22nagercoil%2C%20IND%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys')
weather_json = json.load(weather_api)
data_location = weather_json["query"]['results']['channel']['location']
place = data_location['city']

data_item = weather_json["query"]['results']['channel']['item']
fore_cast = data_item['forecast'][0]['text']
temp = data_item['condition']['temp']
date = data_item['condition']['date']
data_image= weather_json["query"]['results']['channel']['image']
root = Tk()
f = Frame(width=800, height=546, bg='green', colormap='new')

#p = PhotoImage(file=urlopen(file))
##ph =Label(root ,image=p)
w0 = Label(root, fg='#FF6699', text=fore_cast, font=("Helvetica",15))
w = Label(root, fg='blue', text=temp + u'\N{DEGREE SIGN}'+'F', font=("Helvetica",56))
w1 = Label(root, fg='#3399CC', text=place, font=("Helvetica",15))
w0.pack()
w.pack()
w1.pack()
##ph.pack()

root.mainloop()

此代码可能对您使用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或其他变量,因为它已经存在。

代码已更正:

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()

相关问题 更多 >