Python AttributeError:对象没有属性“listbox”?
在这个简单的脚本中,在第1页的列表框中选择的一个项目将从第2页保存和打印。列表框中的值保存在app_数据中。代码运行,但app_数据中未记录任何值。打印的值为空。出现以下异常: Tkinter回调回溯最近调用last时出现异常: 文件/usr/lib/python3.4/tkinter/\uuuuu init\uuuuuu.py,第1536行,在调用中__ 返回self.func*args文件filepath/File.py,第35行,输入 button1=ttk.Buttonself,text=Next Page,command=lambda:controller.show\u framePage2 或self.controller.app_数据[列表框] .setself.listbox.getself.listbox.curs AttributeError:“Page1”对象没有属性“listbox” 据我所知,控制器无法识别列表框。我研究了使用super的可能解决方案,如中所示,但没有成功 问题: 什么可以使列表框值正确保存到app_数据 完整代码:Python AttributeError:对象没有属性“listbox”?,python,python-3.x,tkinter,listbox,Python,Python 3.x,Tkinter,Listbox,在这个简单的脚本中,在第1页的列表框中选择的一个项目将从第2页保存和打印。列表框中的值保存在app_数据中。代码运行,但app_数据中未记录任何值。打印的值为空。出现以下异常: Tkinter回调回溯最近调用last时出现异常: 文件/usr/lib/python3.4/tkinter/\uuuuu init\uuuuuu.py,第1536行,在调用中__ 返回self.func*args文件filepath/File.py,第35行,输入 button1=ttk.Buttonself,text
您的问题很简单:在button1定义中,您使用了self.listbox的一些方法,但是当您定义listbox时,您没有将其定义为Page1的属性:listbox=Listboxself,exportselection=0 no self。在它前面。这就是为什么会出现错误AttributeError:“Page1”对象没有属性“listbox” 有两种方法可以避免这种情况: 如果需要在Page1的_init__方法之外的某个地方使用listbox,请将其设置为一个属性:self.listbox=Listboxself,exportselection=0,并将所有listbox替换为self.listbox 如果您在其他任何地方都不需要它,只需更改按钮1定义中listbox中的self.listbox:
button1 = ttk.Button(self,text="Next Page"
,command=lambda: controller.show_frame(Page2)
or self.controller.app_data["listbox"]
.set(listbox.get(listbox.curselection())))
from tkinter import *
from tkinter import ttk
class MyApp(Tk):
def __init__(self):
Tk.__init__(self)
# App data in controller
self.app_data = {"listbox": StringVar()}
container = ttk.Frame(self)
container.pack(side="top", fill="both", expand = True)
self.frames = {}
for F in (Page1, Page2):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky = NSEW)
self.show_frame(Page1)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class Page1(ttk.Frame):
def __init__(self, parent, controller):
ttk.Frame.__init__(self, parent)
self.controller = controller
listbox = Listbox(self,exportselection=0)
listbox.grid()
for item in [0,1,2,3,4,5]:
listbox.insert(END, item)
button1 = ttk.Button(self,text="Next Page"
,command=lambda: controller.show_frame(Page2)
or self.controller.app_data["listbox"]
.set(self.listbox.get(self.listbox.curselection())))
button1.grid()
class Page2(ttk.Frame):
def __init__(self, parent, controller):
ttk.Frame.__init__(self, parent)
self.controller = controller
ttk.Label(self, text='Next Page').grid(padx=(20,20), pady=(20,20))
button1 = ttk.Button(self, text='Select Page',
command=lambda: controller.show_frame(Page1))
button1.grid()
button2 = ttk.Button(self, text='print value', command=self.print_value)
button2.grid()
def print_value(self):
value = self.controller.app_data["listbox"].get()
print ('The value stored in StartPage some_entry = ' + str(value))
app = MyApp()
app.mainloop()
button1 = ttk.Button(self,text="Next Page"
,command=lambda: controller.show_frame(Page2)
or self.controller.app_data["listbox"]
.set(listbox.get(listbox.curselection())))