简体   繁体   English

如何将 askdirectory 结果保存在我可以将 tkinter 与 OOP 一起使用的变量中?

[英]How to save askdirectory result in a variable I can use using tkinter with OOP?

I have ran into some trouble.我遇到了一些麻烦。 I'm quite new to OOP and working with tkinter and GUI's in general.我对 OOP 很tkinter并且一般使用tkinter和 GUI。

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.我设法在 Internet 上找到了一些代码并将它们组合在一起以创建一些东西,我几乎达到了我想要的位置。

So what I want is some help figuring this out.所以我想要的是一些帮助解决这个问题。

How can I assign results of askdirectory to a variable I can use elsewhere?如何将askdirectory结果分配给我可以在其他地方使用的变量?

# 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 .以上是运行askdirectory代码的调用,它可以工作,只需要找出如何将其保存到变量中以便我可以使用它,我尝试以多种方式打印它,但我得到的只是一些东西.!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()

I had an idea:我有一个想法:

example, if you have f inside a function, you can make it global to have access as variable例如,如果函数中有 f,则可以将其设为全局变量以访问变量

def print_path():  
    # select working directory
    global f #make f global to access the path
    f = filedialog.askdirectory(parent=root, initialdir="/", title='Select Dir') 

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

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