函数无法调用我的新闻属性,因为它说它在类中不存在。
我尝试了很多教程,但似乎没有一个教程。
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语句中使用的函数中。
答案 0 :(得分:0)
发生的事情是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)