访问变量inside方法,python tkinter

时间:2017-07-20 07:48:40

标签: python class methods tkinter

我想在序言中说我已经阅读了docs,我想我不确定如何使用提供的信息,或者我不是能够看到提供的代码和我的代码之间的差异。我也在谷歌搜索相关问题无济于事。我目前正在关注this tutorial

我为我正在制作的简单程序制作了一个tkinter窗口,但现在我试图把它放在课堂上,一切都在走下坡路。有问题的代码的基本结构如下:

from tkinter import *

class WindowManager(Tk):
    def __init__(self, parent):
        Tk.__init__(self, parent)
        self.parent = parent
        # Place items for console log
        self.initialize()

    def initialize(self):
        self.grid()
        self.text_item = Text(self, bd=0, bg="black", fg="white", height="15", width="56", )
        self.text_item.grid(column=0, row=0)

    def access_text(app):
        app.initialize.text_item.delete('1.0', END)


def main():
    app = WindowManager(None)
    app.title("Main Window")
    app.geometry("400x250")
    app.resizable(0, 0)
    app.mainloop()

    access_text(app)


if __name__ == "__main__":
    main()

现在这只是相关代码的基本结构,以及与之相关的所有内容。

窗口关闭时会出现错误"'功能'对象没有属性' text_item'"

我最好的猜测是,它试图通常使用函数执行某些操作而不访问相关函数中的代码,但我不确定访问此变量的正确方法是什么

非常感谢任何帮助或澄清。谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

我的问题是,当它是类

中的局部变量时,我试图从方法中访问变量

而不是app.initialize.text_item我应该只有app.text_item

归功于@Gribouillis