Python:AttributeError:Toplevel1实例没有属性';入口1';
当我尝试打开Python脚本时,会出现以下错误:Python:AttributeError:Toplevel1实例没有属性';入口1';,python,user-interface,tkinter,Python,User Interface,Tkinter,当我尝试打开Python脚本时,会出现以下错误: Traceback (most recent call last): File "gui.py", line 111, in <module> vp_start_gui() File "gui.py", line 30, in vp_start_gui top = Toplevel1 (root) File "gui.py", line 79, in __init__ self.Button1.co
Traceback (most recent call last):
File "gui.py", line 111, in <module>
vp_start_gui()
File "gui.py", line 30, in vp_start_gui
top = Toplevel1 (root)
File "gui.py", line 79, in __init__
self.Button1.configure(command=self.getTemp())
File "gui.py", line 105, in getTemp
this.plz = this.Entry1.get()
AttributeError: Toplevel1 instance has no attribute 'Entry1'
我预计这一行还会出现另一个错误:
self.Text1.configure(text="Stadt: " + str(this.json["name"]) + "\nTemperatur: " + str(this.json["main"]["temp"]))
否则它应该运行。我没有向gui脚本添加太多代码
编辑
好的,现在我有了完整的工作代码:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# GUI module generated by PAGE version 4.22
# in conjunction with Tcl version 8.6
# Apr 24, 2019 01:22:16 PM CEST platform: Darwin
import sys
import requests
import json
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
try:
import ttk
py3 = False
except ImportError:
import tkinter.ttk as ttk
py3 = True
import unknown_support
def vp_start_gui():
'''Starting point when module is the main routine.'''
global val, w, root
root = tk.Tk()
top = Toplevel1 (root)
unknown_support.init(root, top)
root.mainloop()
w = None
def create_Toplevel1(root, *args, **kwargs):
'''Starting point when module is imported by another program.'''
global w, w_win, rt
rt = root
w = tk.Toplevel (root)
top = Toplevel1 (w)
unknown_support.init(w, top, *args, **kwargs)
return (w, top)
def destroy_Toplevel1():
global w
w.destroy()
w = None
class Toplevel1:
key = "xxx" # API KEY von openweather
url = "https://api.openweathermap.org/data/2.5/weather?zip="
parameters = ",DE&units=metric&appid=" + str(key)
data = ""
json = ""
def __init__(self, top=None):
'''self class configures and populates the toplevel window.
top is the toplevel containing window.'''
_bgcolor = '#d9d9d9' # X11 color: 'gray85'
_fgcolor = '#000000' # X11 color: 'black'
_compcolor = '#d9d9d9' # X11 color: 'gray85'
_ana1color = '#d9d9d9' # X11 color: 'gray85'
_ana2color = '#ececec' # Closest X11 color: 'gray92'
top.geometry("144x142+586+143")
top.title("New Toplevel")
top.configure(background="#d9d9d9")
self.Button1 = tk.Button(top)
self.Button1.place(relx=0.208, rely=0.352, height=32, width=77)
self.Button1.configure(activebackground="#ececec")
self.Button1.configure(activeforeground="#000000")
self.Button1.configure(background="#d9d9d9")
self.Button1.configure(cursor="fleur")
self.Button1.configure(foreground="#000000")
self.Button1.configure(highlightbackground="#d9d9d9")
self.Button1.configure(highlightcolor="black")
self.Button1.configure(text='''GET''')
self.Button1.configure(width=77)
self.Button1.configure(command= lambda: self.getTemp())
self.Entry1 = tk.Entry(top)
self.Entry1.place(relx=0.069, rely=0.07,height=27, relwidth=0.847)
self.Entry1.configure(background="white")
self.Entry1.configure(cursor="fleur")
self.Entry1.configure(font="TkFixedFont")
self.Entry1.configure(foreground="#000000")
self.Entry1.configure(insertbackground="black")
self.Entry1.configure(width=122)
self.Text1 = tk.Text(top)
self.Text1.place(relx=0.069, rely=0.634, relheight=0.296, relwidth=0.889)
self.Text1.configure(background="white")
self.Text1.configure(font="TkTextFont")
self.Text1.configure(foreground="black")
self.Text1.configure(highlightbackground="#d9d9d9")
self.Text1.configure(highlightcolor="black")
self.Text1.configure(insertbackground="black")
self.Text1.configure(selectbackground="#c4c4c4")
self.Text1.configure(selectforeground="black")
self.Text1.configure(width=128)
self.Text1.configure(wrap="word")
def getTemp(self):
self.plz = self.Entry1.get()
self.data = requests.get(self.url + str(self.plz) + self.parameters)
self.json = json.loads(self.data.text)
self.Text1.insert(tk.END, "Stadt: " + str(self.json["name"]) + "\nTemperatur: " + str(self.json["main"]["temp"]))
if __name__ == '__main__':
vp_start_gui()
您在
\uuuu init\uuuu
中分配了self.Entry1=tk.Entry(顶部)
。然后通过getTemp(this)
方法中的this.Entry1…
引用条目。此操作失败,因为只能在self
下找到Entry1
。将所有this
更改为self
,应该可以。您在\uuuu init\uuu
中指定的self.Entry1=tk.Entry(顶部)
。然后通过getTemp(this)
方法中的this.Entry1…
引用条目。此操作失败,因为只能在self
下找到Entry1
。将所有this
更改为self
,应该可以。您想要的是self
,而不是this
self.plz = self.Entry1.get()
这是Python,不是Java。(还要注意,名称self
作为第一个参数显式传递到方法中。理论上,您可以使用this
作为名称,而不是self
,但后者是绝对通用的标准,因此请不要使用。)
更新:
你的线路
self.Button1.configure(command=self.getTemp())
先于
self.Entry1 = tk.Entry(top)
因此,在定义
self.Entry1
之前,正在调用self.getTemp()
。您想要的是self
,而不是此
self.plz = self.Entry1.get()
这是Python,不是Java。(还要注意,名称self
作为第一个参数显式传递到方法中。理论上,您可以使用this
作为名称,而不是self
,但后者是绝对通用的标准,因此请不要使用。)
更新:
你的线路
self.Button1.configure(command=self.getTemp())
先于
self.Entry1 = tk.Entry(top)
因此,在定义self.Entry1
之前调用了self.getTemp()
。一些编辑:
- 将此
更改为
。尝试为整个类使用一个唯一的关键字self
- 对于
为按钮1配置,请使用:命令
或self.Button1.configure(command=self.getTemp)#不带paradensis
self.Button1.configure(命令=lambda:self.getTemp())
- 将此
更改为
。尝试为整个类使用一个唯一的关键字self
- 对于
为按钮1配置,请使用:命令
或self.Button1.configure(command=self.getTemp)#不带paradensis
self.Button1.configure(命令=lambda:self.getTemp())
this
而不是self
作为getTemp
的参数,然后以相同的方法使用self
而不将其作为参数传递?全部更改为self
,仍然不起作用。但谢谢你,这是一个尝试的好主意。我想回溯现在会有所不同。不。它仍然是getTemp self.plz=self.Entry1.get()AttributeError:Toplevel1实例没有属性“Entry1”我不确定,但可能是因为您正在使用this
而不是self
作为getTemp
的参数,然后在相同的方法中使用self
而不将其作为参数传递?将所有更改为self
,仍然不起作用。但谢谢你,这是一个尝试的好主意。我想回溯现在会有所不同。不。它仍然是getTemp self.plz=self.Entry1.get()AttributeError:Toplevel1实例没有属性“Entry1”就是这样!谢谢!:)@123php只是为了解释为什么需要删除paranthesis—编译器将运行该命令(如果存在paranthesis),并且条目小部件仍然没有定义。这就是错误所在,就是这样!谢谢!:)@123php只是为了解释为什么需要删除paranthesis—编译器将运行该命令(如果存在paranthesis),并且条目小部件仍然没有定义。因此出现了错误。