0

My news attribute can't be called by a function as it says it doesn't exist as an attribute in the class.

I have tried a lot of tutorials but none seem it include a class.

from tkinter import *


class Interface(Frame):
    def __init__(self, parent, *args, **kwargs):
        Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        self.master.title("Travel Information")

        # Labels
        self.main_label = Label(text="Travel Information", font='Helvetica 18 bold')
        self.main_label.grid(column=1, row=0)

        self.desc_label = Label(text="This program will gather a variety of information\n on a "
                                     "location of the users choice.")
        self.desc_label.grid(column=0, row=1, columnspan=3)

        self.entry_label = Label(text="Enter the location:")
        self.entry_label.grid(column=0, row=2, sticky=W)

        # Entries
        self.location_entry = Entry()
        self.location_entry.grid(column=1, row=2, pady=5, padx=6, sticky=W+E)

        # Buttons
        self.location_submit_button = Button(text="Submit", width=10, command=self.get_location())
        self.location_submit_button.grid(column=2, row=2, pady=20)

        self.quit_button = Button(text="Quit", command=quit)
        self.quit_button.grid(column=1, row=7)

        # Check Buttons
        self.weather = IntVar()
        self.weather_checkbutton = Checkbutton(text="Weather Info", variable=self.weather)
        self.weather_checkbutton.grid(column=1, row=3, sticky=W)

        self.tourist = IntVar()
        self.tourist_checkbutton = Checkbutton(text="Tourist Info", variable=self.tourist)
        self.tourist_checkbutton.grid(column=1, row=4, sticky=W)

        self.transport = IntVar()
        self.transport_checkbutton = Checkbutton(text="Transport Info", variable=self.transport)
        self.transport_checkbutton.grid(column=1, row=5, sticky=W)

        self.news = IntVar()
        self.news_checkbutton = Checkbutton(text="News Info", variable=self.news)
        self.news_checkbutton.grid(column=1, row=6, sticky=W)

    # Logic Functions (To be moved)

    def get_location(self):
        location = self.location_entry.get()
        news = self.news.get()
        transport = self.transport.get()
        tourist = self.tourist.get()
        weather = self.weather.get()
        print(location)

        if news == 0 and transport == 0 and tourist == 0 and weather == 0:
            print("None Selected Working")

        elif news == 0 and transport == 0 and tourist == 0 and weather == 1:
            print("Weather working")

        elif news == 0 and transport == 0 and tourist == 1 and weather == 0:
            print("Tourist Working")

        elif news == 0 and transport == 0 and tourist == 1 and weather == 1:
            print("Tourist and Weather working")

        elif news == 0 and transport == 1 and tourist == 0 and weather == 0:
            print("Transport working")

        else:
            print()

        return location

root = Tk()
root.geometry("")
app = Interface(root)
root.mainloop()

The Error: Traceback (most recent call last):

  File "C:/Users/Scott/Desktop/Individual Project/IndProj.py", line 89, in <module>
    app = Interface(root)
  File "C:/Users/Scott/Desktop/Individual Project/IndProj.py", line 32, in __init__
    self.location_submit_button = Button(text="Submit", width=10, command=self.get_location())
  File "C:/Users/Scott/Desktop/Individual Project/IndProj.py", line 59, in get_location
    news = self.news.get()
AttributeError: 'Interface' object has no attribute 'news'

I expected the function to bring the travel, news, tourist and weather variables into the function to be used in the if statement.

stovfl
  • 14,998
  • 7
  • 24
  • 51
Scotto
  • 13
  • 2

1 Answers1

0

What happened is that Python threw an an exception at that line which broke the execution of the code so the news variable was not set

change

self.location_submit_button = Button(text="Submit", width=10, command=self.get_location())

into

self.location_submit_button = Button(text="Submit", width=10, command=self.get_location)
Lee
  • 1,427
  • 9
  • 17
Amine Messaoudi
  • 2,141
  • 2
  • 20
  • 37