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

时间:2017-01-13 02:50:46

标签: python user-interface tkinter

我正在尝试使用Python 3.5,Tkinter和Weather Underground API创建天气应用程序。我正在尝试放入一个Entry窗口小部件,用户可以在该窗口小部件中输入。我正在制作它,以便当用户点击进入时,它将显示输入位置的温度和当前条件。这是我应该创建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(最近一次调用最后一次):文件   &#34; C:/用户/ jsorh / OneDrive /文档/学校/网络   Design / weather / App / WeatherApp.py&#34;,第70行,in       窗口()文件&#34; C:/ Users / jsorh / OneDrive / Documents / School / Web Design / weather / App / WeatherApp.py&#34;,第20行, init       self.locationEntry.bind(&#34;&#34;,self.onEnter())文件&#34; C:/ Users / jsorh / OneDrive / Documents / School / Web   设计/天气/ App / WeatherApp.py&#34;,第26行,在onEnter上       self.place = self.place.get(self)File&#34; C:\ Users \ jsorh \ AppData \ Local \ Programs \ Python \ Python35-32 \ lib \ tkinter__init __。py&#34;,   第333行,在得到       value = self._tk.globalgetvar(self._name)AttributeError:&#39; Window&#39;对象没有属性&#39; _tk&#39;

我在互联网上寻找一些解决方案,但我真的不明白如何解决这个问题。我是编程的初学者,所以请尽可能简单地解释一下。感谢。

2 个答案:

答案 0 :(得分:1)

在您的代码中存在以下错误:

  • 由于您正在创建对象,因此必须将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()

答案 1 :(得分:0)

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

此代码可能会为您使用