简体   繁体   English

在更高范围内访问python类属性

[英]Accessing python class attributes in higher scope

Still learning Python, rookie question here. 仍在学习Python,菜鸟问题在这里。 I'm building a simple tkinter GUI that loads JSON data and then allows the user to select loaded data for plotting. 我正在构建一个简单的tkinter GUI,该GUI加载JSON数据,然后允许用户选择加载的数据进行绘图。 Here's a description of my architecture and issue: 这是我的体系结构和问题的描述:

1) In the GUI, I have a load button that calls a function, LoadData, to open a directory containing multiple JSON txt files 1)在GUI中,我有一个加载按钮,调用一个函数LoadData,以打开包含多个JSON txt文件的目录

2) LoadData checks to see if a certain file name exists, 'AutoPilot.txt', and if it does, another function, LoadAutopilotData, is called 2)LoadData检查是否存在某个文件名“ AutoPilot.txt”,如果存在,则调用另一个函数LoadAutopilotData

3) LoadAutopilotData creates an instance of the Data class and populates it with the JSON data 3)LoadAutopilotData创建Data类的实例,并用JSON数据填充

After the functions are complete, I want to access the instance of the Data class I created in the inner scope (APtime), but I can't seem to do that. 功能完成后,我想访问在内部作用域(APtime)中创建的Data类的实例,但似乎无法做到这一点。

File 1 snippet: 文件1的摘要:

loadBtn = Button(toolbar, image=useImg1, command=LoadData)

File 2 snippet: 文件2片段:

def LoadData() :

    # Get data Path from the User
    path = askdirectory()

    # Go to that directory
    os.chdir(path)

    # Check directory to see if AutoPilot.log is available
    try:
        Autopilot = open("AutoPilot.txt")
        Load_Autopilot = True
    except:
        Load_Autopilot = False

    # If Autopilot data exists, load it and populate the listbox
    if Load_Autopilot == True:
        LoadAutopilotData()
        print(APtime.val)

File 3 snippet: 文件3片段:

def LoadAutopilotData() :

    filedata = open( 'AutoPilot.txt' )

    if len( sys.argv) >= 2:
        controller = sys.argv[1]

    APtime = Data("Time", [], "sec")

where File3 continues on to populate an instance of the Data class. File3继续在其中填充Data类的实例。 The problem I'm having is that I can only access APtime in File3, and not in the 'higher' level functions. 我遇到的问题是我只能在File3中访问APtime,而不能在“更高”级别的函数中访问。 Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks! 谢谢!

What you need to do is return a value to the calling function. 您需要做的是向调用函数返回一个值。 You can look at the tutorial section on Defining functions on the Python website to have a little more detail about that. 您可以查看Python网站上有关定义函数的教程部分,以获取有关此内容的更多详细信息。

So if we don't change your code too much this could loook like that: 因此,如果我们不对您的代码进行过多更改,则可能会出现以下情况:

Solution 1: File 2 snippet 解决方案1:文件2片段

def LoadData() :

    # Get data Path from the User
    path = askdirectory()

    # Go to that directory
    os.chdir(path)

    # Check directory to see if AutoPilot.log is available
    try:
        Autopilot = open("AutoPilot.txt")
        Load_Autopilot = True
    except:
        Load_Autopilot = False

    # If Autopilot data exists, load it and populate the listbox
    if Load_Autopilot == True:
        APtime = LoadAutopilotData()
        print(APtime.val)

Solution 1: File 3 snippet 解决方案1:文件3片段

def LoadAutopilotData() :

    filedata = open( 'AutoPilot.txt' )

    if len( sys.argv) >= 2:
        controller = sys.argv[1]

    APtime_result = Data("Time", [], "sec")

    return APtime_result
  • You should see that the function LoadData() assign the value returned by the function LoadAutopilotData() to the APtime variable (the line APtime = LoadAutopilotData() ) 您应该看到函数LoadData()将函数LoadAutopilotData()返回的值分配给APtime变量(行APtime = LoadAutopilotData() )。
  • After the function LoadAutopilotData() process its data, it uses the statement return APtime_result to make that value available to the calling function. 函数LoadAutopilotData()处理其数据后,它将使用语句return APtime_result使该值可用于调用函数。

But there are some improvement that could be done in your code. 但是您的代码中可以做一些改进。 The only one I will mention is that you should read what is in the Standard Library because it could save you some work. 我唯一要提到的是您应该阅读标准库中的内容,因为它可以节省您一些工作。 For example, to check if a file exists, there's a function that already exists for that. 例如,要检查文件是否存在,有一个已经存在的功能

So your File 2 snippet could look like that: 因此,您的File 2代码段可能如下所示:

import os.path

def LoadData() :

    # Get data Path from the User
    path = askdirectory()

    Load_Autopilot = os.path.exists(path)

    # If Autopilot data exists, load it and populate the listbox
    if Load_Autopilot:
        APtime = LoadAutopilotData()
        print(APtime.val)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM