How to save askdirectory result in a variable I ca

2019-03-04 07:20发布

问题:

I have ran into some trouble. I'm quite new to OOP and working with tkinter and GUI's in general.

I have managed to find some code on the Internet and meshed it all together to create something and I'm nearly where I want to be.

So what I want is some help figuring this out.

How can I assign results of askdirectory to a variable I can use elsewhere?

# coding=utf-8
import tkinter as tk
from tkinter import font as tkfont
from tkinter import filedialog


class MainApp(tk.Tk):
    ....
class SelectFunction(tk.Frame):
    ....
class FunctionChangeName(tk.Frame):
    ....
        a = Gui(self)
        # this gets me the askdirectory but how to add it to a variable?

Above is the call to run askdirectory code, and it works, just need to find out how to save it to a variable so I can use it, I have tried to print it in several ways, but all I get is something along the lines .!frame.!functionchangename.!gui.

class SelectDir:
    def __init__(self, container, title, initial):
        self.master = container

        self.initial = initial
        self.selected = initial

        self.options = {'parent': container,'title': title,'initialdir':initial,}

    def show(self):
        result = filedialog.askdirectory()
        if result:
            self.selected = result

    def get(self):
        return self.selected


class Gui(tk.Frame):

    def __init__(self, container):
        tk.Frame.__init__(self, container)

        frame = tk.Frame(container)
        frame.pack()

        self.seldir = SelectDir(self, "Select directory", "D:\\MyPgm\\Python\\Tiles_8")

        button = tk.Button(frame, text="Select directory", command=self.select_dir)
        button.grid(column=0, row=0)

        self.act_dir = tk.StringVar()
        self.act_dir.set("D:\\MyPgm\\Python\\Tiles_8")

        entry = tk.Entry(frame, textvariable=self.act_dir, width=30)
        entry.grid(column=0, row=1)

    def select_dir(self):
        self.seldir.show()
        self.act_dir.set(self.seldir.get())
        # or
        # result = seldir.show()
        # self.act_dir.set(result)

if __name__ == "__main__":
    app = MainApp()
    app.mainloop()