【问题标题】:Attribute error: Error says new attribute doesn't exist even though it does [duplicate]属性错误:错误说新属性不存在,即使它确实存在[重复]
【发布时间】:2019-05-06 12:45:51
【问题描述】:

我的新闻属性不能被函数调用,因为它说它在类中不作为属性存在。

我尝试了很多教程,但似乎没有一个包含课程。

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

错误:回溯(最后一次调用):

  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'

我希望该函数将旅行、新闻、旅游和天气变量带入函数中以在 if 语句中使用。

【问题讨论】:

  • 能否在您的问题中包含错误回溯?
  • 添加错误
  • 这是因为您将self.get_location() 传递给location_submit_button 的命令选项,而此时self.news 尚未创建。您应该改为传递函数的引用。
  • 因为最后的 () 你必须删除它们
  • 是的,去掉括号就解决了。谢谢大家

标签: python class tkinter


【解决方案1】:

发生的事情是 Python 在该行抛出异常,中断了代码的执行,因此未设置 news 变量

改变

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

进入

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-08
    • 2018-11-12
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 2020-01-22
    • 1970-01-01
    • 2018-01-29
    相关资源
    最近更新 更多