Spyder 3.7 - 如何将值存储在函数的变量中

Spyder 3.7 - How to store value in a variable from a function

我意识到这可能真的很简单,我一直都是在 Matlab 中做的。现在,我想要做的就是使用 tkinter 中的按钮打开文件对话框,选择文件,并将路径名存储在变量中。

class Functions:
    def FileDialog(Pathname):
        Pathname = tkFileDialog.askopenfilename()

Funks = Functions()
Btn1 = tkinter.Button(MainWindow, text = "Browse", command = Funks.FileDialog)

函数完成后,"Pathname"去哪了?如何在变量资源管理器中查看它?

更新:我现在以不同的方式构建代码,这里是完整的

##########################################
import tkinter as tk                     #
from tkinter import Frame                #
import tkinter.filedialog as tkFileDialog#
##########################################

class Window(Frame):
    def __init__(self, master = None):
        Frame.__init__(self, master)
        self.master = master
        self.MainWindow()

    def MainWindow(self):
        self.master.title("The SHIFT Show"); self.pack() 
        Btn1 = tk.Button(self, text = "Browse", command = self.FileDialog)
        Btn1.place(x = 0, y = 0); Btn1.pack()
        Btn2 = tk.Button(self, text = "Close Window", command = self.Exit) 
        Btn2.place(x = 0, y = 0); Btn2.pack()
        Btn3 = tk.Button(self, text = "Pathname?", command = self.PathTest)
        Btn3.place(x = 0, y = 0); Btn3.pack()

    def FileDialog(self):
        Pathname = tkFileDialog.askopenfilename()
        return Pathname

    def Exit(self):
        exit() #DOESNT WORK

    def PathTest(self,Pathname):
        print(Pathname)

root = tk.Tk()
app = Window(root)
root.geometry("500x500+2500+1100")
root.mainloop()

一般来说,你不会。 Pathname 是局部变量。根据定义,一旦退出函数,它就不存在了。调用 space 从 运行 时间堆栈弹出,内存被释放和回收,并且 none 的信息不再可用。

如果你想使用那个值,你需要以某种方式将它转移到那个函数之外。典型的方法包括:

  • 打印出来(对人类有用;对您的代码的其余部分没有用)
  • return它在函数的末尾
  • 传入可变参数;将Pathname存入对应参数

最后一个需要能够更改 FileDialog 的签名(调用配置文件),并能够向其推送第二个参数。

您的 Functions class 无法正常工作,因为您尚未将其方法设置为作为方法工作。一个普通的方法需要期望 class 的实例作为第一个参数传入(按照惯例,这个参数被命名为 self,尽管这并不是严格要求的)。

有了 self 参数后,您可以在其上创建属性来存储传递到您的方法中的数据。这可能是您在这里寻找的。

试试这个:

class Functions:
    def file_dialog(self):
        self.pathname = tkFileDialog.askopenfilename()

现在,如果您创建一个名为 funks 的实例并将 funks.file_dialog 作为回调传递给您的按钮,它将把用户选择的文件名保存为 funks.pathname。请注意,我已经稍微重命名了您的几个函数和变量名称,以便更好地映射到 Python 典型样式,其中 CapitalizedNames 仅用于 classes 和大多数其他变量(和函数) ) 给出 lower_case_names_with_underscores.

请注意,您可能希望以不同的方式构建代码,而不是将 class 命名为 Functions,因为 class 的目的实际上应该是保留一堆 data一起(连同相关方法)。 类 通常不只是用作放置不相关函数的命名空间。如果您只需要一个命名空间,那么模块是显而易见的选择。我认为 class does 在你在这里询问的情况下是有意义的,但你应该在命名 class 时记住它的数据!如果您在代码的不同部分处理不相关的数据,这表明您可能需要对这些部分使用不同的 classes。

我让它做我想做的事。耶。

##########################################
import tkinter as tk                     #
from tkinter import Frame                #
import tkinter.filedialog as tkFileDialog#
##########################################

class Window(Frame):
    def __init__(self, master = None):
        Frame.__init__(self, master)
        self.master = master
        self.MainWindow()

    def MainWindow(self):
        self.master.title("The SHIFT Show"); self.pack() 
        # BUTTONS
        ## File dialog button
        Btn1 = tk.Button(self, text = "Browse", command = self.FileDialog)
        Btn1.place(x = 0, y = 0); Btn1.pack()
        ## Close window button
        Btn2 = tk.Button(self, text = "Close Window", command = self.Exit) 
        Btn2.place(x = 0, y = 0); Btn2.pack()
        ## Test button for....things
        Btn3 = tk.Button(self, text = "Pathname?", command = self.PathTest)
        Btn3.place(x = 0, y = 0); Btn3.pack()

    def FileDialog(self):
        global Pathname
        folderpath = tkFileDialog.askdirectory()
        Pathname = folderpath        

    def Exit(self):
        exit() # STILL DOESNT WORK

    def PathTest(self):
        print(Pathname) # Outputs the folder path to the console (so I know it's there)

root = tk.Tk()
app = Window(root)
root.geometry("500x500+2500+1100")
root.mainloop()