python - 属性错误: object has no attribute 'listbox' ?

标签 python python-3.x tkinter listbox

在这个简单的脚本中,在 Page1 上的 listbox 中选择的项目将被保存并从 Page2 打印。 listbox 中的值保存在 app_data 中。代码运行,但 app_data 中没有记录任何值(打印的值是空白)。出现以下异常:

Exception in Tkinter callback Traceback (most recent call last):
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1536, in __call__
    return self.func(*args)   File "filepath/file.py", line 35, in <lambda>
    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())))
AttributeError: 'Page1' object has no attribute 'listbox'

据我所知, Controller 无法识别列表框。我使用 super() 研究了可能的解决方案,如 Understanding Python super() with init() methods 所示。但还没有成功。

问题: 什么将使列表框值正确保存到 app_data

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

完整代码:

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 定义中,你使用了 self.listbox 的一些方法,但是当你定义列表框时,你没有将其定义为属性Page1 的:listbox = Listbox(self,exportselection=0)(前面没有 self.)。这就是为什么您收到错误 AttributeError: 'Page1' object has no attribute 'listbox'

有两种方法可以避免这种情况:

  • 如果您需要在 Page1__init__ 方法之外的某个地方使用列表框,请将其设为属性:self.listbox = Listbox(self ,exportselection=0) 并将所有 listbox 替换为 self.listbox

  • 如果您在其他地方不需要它,只需更改 button1 定义中 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())))
    

关于python - 属性错误: object has no attribute 'listbox' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42125227/

相关文章:

python - pip 错误 : unrecognized command line option ‘-fstack-protector-strong’

Python:从文本文件中提取主题标签

python - PyOpenCL 索引问题

python-3.x - 如何使用OpenCV读取文件夹中的第一张图像(Python)

python - 如何在Windows 10 中备份anaconda 环境?

python - 从字符串中删除 "\n",但保留实际的换行符?

python - 扁平大阵列的 Numpy 平均值比所有轴的平均值慢

python - 如何处理OSError : [Errno 36] File name too long

python - 如何在Python中的tk标签上显示网页中的png文件?

python - 需要让 tkinter 消息框根据条件显示消息