AttributeError:类型对象'class'没有属性'stringVar'

时间:2016-07-15 15:13:50

标签: python python-3.x tkinter

我正在尝试制作一个包含多个带按钮和标签页面的GUI程序。我需要程序能够通过按下按钮动态更改标签 与我目前没有工作的页面上的标签进行交互。我已经制作了一个运行的示例程序,但是在尝试更改未显示的页面中的标签时出现错误。

import tkinter as tk
from tkinter import ttk

LARGE_FONT = ("Verdana", 12)


class ManyPagesApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}

        # This loop adds the pages into the frame
        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)

    # This function raises the page to the "top" level of the frame
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


# Creates what shows in start page
class Page1(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        button1 = ttk.Button(self, text="Go to Page 2", command=lambda: controller.show_frame(Page2))
        button1.pack()

        button2 = ttk.Button(self, text="THIS BUTTON CHANGES LABEL ON PAGE 1",
                         command=lambda: self.labelChanger('This label was changed by using button 2'))
        button2.pack()

        label = ttk.Label(self, text='This is Page 1', font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        self.labelVariable1 = tk.StringVar()
        self.labelVariable1.set('This label will be changed')
        label = tk.Label(self, textvariable= self.labelVariable1)
        label.pack()

    def labelChanger(self, text):
        self.labelVariable1.set(text)

class Page2(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        objectofpage1 = Page1

        button1 = ttk.Button(self, text="Goes to page 1", command=lambda: controller.show_frame(Page1))
        button1.pack()

        button2 = ttk.Button(self, text="Changes Label 2 in page 1",
                         command=lambda: objectofpage1.labelChanger(objectofpage1, 'You have changed the label in page 1'))
        button2.pack()

        label = ttk.Label(self, text='This is page 2', font=LARGE_FONT)
        label.pack(pady=10, padx=10)



# Runs everything
if __name__ == "__main__":
    app = ManyPagesApp()
    app.mainloop()

由于有更多页面,我的应用程序变得更大但我制作了这个示例程序,它给了我同样的错误。

OBS:只有在第2页上尝试更改第1页上的标签时才会出现错误。

这是弹出的错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "***\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
  File "***/TestApp.py", line 63, in <lambda>
command=lambda: objectofpage1.labelChanger(objectofpage1, 'You have changed the label in page 1'))
  File "***/TestApp.py", line 51, in labelChanger
self.labelVariable1.set(text)
AttributeError: type object 'Page1' has no attribute 'labelVariable1'

PS:第一次发帖,我不知道这是否已经得到解答,但我找不到任何解决这个问题的方法,所以我创建了一个账号来发帖。

我很感激帮助,并提前致谢!

1 个答案:

答案 0 :(得分:1)

错误在于此声明:

objectofpage1 = Page1

您正在将objectofpage1设置为,而不是类的实例。您的程序已经有一个实例,您只需要获取由控制器管理的实例。

如果每个页面都需要访问其他页面,则只需向控制器添加一个方法即可返回实例。像这样:

class ManyPagesApp(tk.Tk):
    ...
    def get_page(self, page_class):
        return self.frames[page_class]

class Page2(tk.Frame):

    def __init__(self, parent, controller):
        ...
        objectofpage1 = controller.get_page(Page1)

注意:您在此声明中也有一个错误:

command=lambda: objectofpage1.labelChanger(objectofpage1, 'You have changed the label in page 1'))

应该是:

command=lambda: objectofpage1.labelChanger('You have changed the label in page 1'))

有关在页面之间共享信息的更全面讨论,请参阅How to get variable data from a class