【问题标题】:How to access an object defined in a function from another function?如何从另一个函数访问一个函数中定义的对象?
【发布时间】:2017-03-30 22:03:40
【问题描述】:

我正在构建一个简单的 GUI 应用程序来管理优先级。我坚持从另一个函数访问一个函数中定义的对象,在这种情况下,从 display() 访问函数 createWindow 中的 root .窗口打开但没有标签,这是我退出程序时给我的:

/usr/bin/python3.5 /home/cali/PycharmProjects/priorities/priorities.py Traceback(最近一次调用最后一次):文件 “/home/cali/PycharmProjects/priorities/priorities.py”,第 34 行,在 c.main() 文件“/home/cali/PycharmProjects/priorities/priorities.py”,第 31 行,在 主要的 g.display() 文件“/home/cali/PycharmProjects/priorities/priorities.py”,第 22 行,在 展示 Label(root, NameError: name 'root' is not defined

进程以退出代码 1 结束

这是我到目前为止所做的:

# priorities.py
#   GUI program to manage priorities

from tkinter import *

class Priority:
    pass

class GuiPart:

    def createWindow(self):

        root = Tk()
        root.resizable(width = False, height = False)
        root.title = "Priorities"
        root.mainloop()

        return root

    def display(self):

        Label(root,
              text = "testes").grid(row = 0, column = 1)

class Client:

    def main(self):

        g = GuiPart()
        g.createWindow()
        g.display()

c = Client()
c.main()

我使用的是 Python 3.6。

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    我认为您可能想要做的是在调用 createWindow() 后将 root 存储为 GuiPart 类的实例变量:

    class GuiPart:
        def __init__(self):
            self.root = createWindow()
    
        def createWindow(self):
    
            root = Tk()
            root.resizable(width = False, height = False)
            root.title = "Priorities"
            root.mainloop()
    
            return root
    
        def display(self):
    
            Label(self.root,
                  text = "testes").grid(row = 0, column = 1)
    

    现在发生的情况是,当您创建 GuiPart 实例时,root 被存储为 g.root。您的主要功能将如下所示:

    def main(self):
    
        g = GuiPart()
        g.display()
    

    你也可以这样做:

    class GuiPart:
        def __init__(self):
            self.root = None
    
        def createWindow(self):
    
            root = Tk()
            root.resizable(width = False, height = False)
            root.title = "Priorities"
            root.mainloop()
    
            self.root = root
    
        def display(self):
    
            Label(self.root,
                  text = "testes").grid(row = 0, column = 1)
    

    那么你的主要是这样的:

    def main(self):
        g = GuiPart()
        g.createWindow()
        g.display()
    

    【讨论】:

    • 第一个版本给出了 NameError: name 'createWindow' is not defined,而第二个版本在退出时给出了这个:不能调用“label”命令:应用程序已被销毁,你忘了 (? ) 主要(自我)
    • 听起来您的代码中除了原始问题中的问题之外还有其他问题。
    • 是的,确实如此。对不起。
    • 不用担心,我建议阅读有关 TK 的教程,这可能会让 IMO 感到困惑。 tkdocs.com/tutorial
    【解决方案2】:

    你想在这里使用类属性,像这样

    class GuiPart:
        def createWindow(self):
            root = Tk()
            root.resizable(width=False, height=False)
            root.title = "Priorities"
            root.mainloop()
    
            self.root = root
    
            return root
    
    
        def display(self):
            Label(self.root,
                  text="testes").grid(row=0, column=1)
    

    注意root 的值如何分配给self.root,然后再次从self.root 读取。 self 可用于在对象的生命周期内存储对象属性。

    【讨论】:

    • AttributeError: 'GuiPart' 对象没有属性 'root'
    猜你喜欢
    • 2022-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    相关资源
    最近更新 更多