Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何在Python2.7中从函数中提取变量_Python 2.7_User Defined Functions - Fatal编程技术网

Python 2.7 如何在Python2.7中从函数中提取变量

Python 2.7 如何在Python2.7中从函数中提取变量,python-2.7,user-defined-functions,Python 2.7,User Defined Functions,我已经在Python2.7.3中编写了一些代码,我希望在函数中生成的某个变量能够被提取出来,并在以后的程序中使用。我已经为变量指定了全局定义,但仍然没有成功。代码如下: def target_fileopen(): dirname = ' ' dlg = wx.FileDialog(None, "Select file", dirname, "", "*.txt", wx.OPEN) if dlg.ShowModal() == wx.ID_OK:

我已经在Python2.7.3中编写了一些代码,我希望在函数中生成的某个变量能够被提取出来,并在以后的程序中使用。我已经为变量指定了全局定义,但仍然没有成功。代码如下:

def target_fileopen():
     dirname = ' ' 
     dlg = wx.FileDialog(None, "Select file", dirname, "", "*.txt", wx.OPEN)
     if dlg.ShowModal() == wx.ID_OK:
         filename = dlg.GetFilename()
         dirname = dlg.GetDirectory()
         coordfile = open(os.path.join(dirname, filename), 'r')
         dlg.Destroy()

     global path
     path = str(dirname + "\\" + filename)
     return target_calc(coordfile)
     return(path)
print(path)
我得到的错误是没有定义全局名称“path”

这只是一个示例,我实际上并不想打印路径,只想将其作为静态文本放置在应用程序的GUI部分

任何帮助都将不胜感激。

在我的电脑上都可以。 我可以访问函数中定义的全局变量。
在访问变量之前是否调用了该方法?

您不能让函数返回该变量吗?但是您还没有在代码主体中调用函数。。。尝试打印(target_fileopen())而不是打印(path)。按下按钮调用函数。按下按钮时,将显示对话框。选择文件并在对话框中按open后,我希望打印路径。实际上,我需要它接受StaticText的label参数-->text=wx。StaticText(win,label=path)删除行“global path”,然后在程序的其余部分调用target_fileopen(),而不是path,因为它返回path。因此,“text=wx.StaticText(win,label=target_fileopen())”当我以这种方式运行它时,文件对话框立即出现,而不是guit。该方法通过按下按钮来调用。按下按钮时,对话框出现。选择文件并在对话框中按open后,我希望打印路径