0

When I try to open my Python script, it gives me this error:

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'

I googled the error but I haven't got any similar results. Also, the attribute Entry1 exists as you can see in the code. And I'm using the correct command to access Entry1.

Full code:

#! /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:
    def __init__(self, top=None):
        '''This 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'
        key = "xxx" # API KEY von openweathermaps eintragen
        url = "https://api.openweathermap.org/data/2.5/weather?zip="
        parameters = ",DE&units=metric&appid=" + str(key)
        data = ""
        json = ""

        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=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.configure(text="Stadt: " + str(self.json["name"]) + "\nTemperatur: " + str(self.json["main"]["temp"]))

if __name__ == '__main__':
    vp_start_gui()

I expect another error with this line:

self.Text1.configure(text="Stadt: " + str(this.json["name"]) + "\nTemperatur: " + str(this.json["main"]["temp"]))

Otherwise it should run. I didn't add much code to the gui script.

EDIT Ok, now I have the full working code:

#! /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()
123php
  • 307
  • 1
  • 4
  • 17
  • I'm not sure, but maybe it's because you're using `this`instead of `self`as an argument for `getTemp` and then use `self`in the same method without passing it as an argument? – Original BBQ Sauce Apr 24 '19 at 12:35
  • Changed all to `self`, still not working. But thank you, this was a good idea to try. – 123php Apr 24 '19 at 12:38
  • 1
    I imagine that the traceback will be different now. – brunns Apr 24 '19 at 12:42
  • No. It's still `File "gui.py", line 105, in getTemp self.plz = self.Entry1.get() AttributeError: Toplevel1 instance has no attribute 'Entry1'` – 123php Apr 24 '19 at 12:51

3 Answers3

2

A couple of edits:

  • Change this to self. Try to use one unique keyword for the entire class.
  • For the command configure for Button1, either use:

    self.Button1.configure(command = self.getTemp) #without the paranthesis OR self.Button1.configure(command = lambda: self.getTemp())

FrainBr33z3
  • 1,085
  • 1
  • 6
  • 12
  • That's it! Thanks! :) – 123php Apr 24 '19 at 12:49
  • 1
    @123php just to explain why you need to remove paranthesis - the compiler will run the command (if paranthesis is present) and the Entry widget is still not defined. Hence the error. – FrainBr33z3 Apr 24 '19 at 12:55
0

You assigned self.Entry1 = tk.Entry(top) in your __init__. Then you reference the entry by this.Entry1 ... in your getTemp(this) method. This fails because Entry1 can only be found under self. Change all this to self and it should do.

woodz
  • 737
  • 6
  • 13
0

You want self, not this.

self.plz = self.Entry1.get()

This is Python, not Java. (Note also that the name self is explicitly passed into the method as a first argument. In theory, you could use this as the name, rather than self, but the latter is an absolutely universal standard, so please don't.)

Update:

Your line

self.Button1.configure(command=self.getTemp())

comes before

self.Entry1 = tk.Entry(top)

So, self.getTemp() is being called before self.Entry1 is defined.

brunns
  • 2,689
  • 1
  • 13
  • 24