如何保存“自我"?当B类获得由C类返回的值时,将值从B类的变量返回到A类: [英] How to save "self" return value to a variable from Class B: to Class A when Class B gets that value returned by Class C

查看:62
本文介绍了如何保存“自我"?当B类获得由C类返回的值时,将值从B类的变量返回到A类:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先是OOP编码的新手,很抱歉提出愚蠢的问题.

我在从 Class AskDir 返回的值(如果有道理)从 Class SelectDir 获取的值到 Class MainWindow 时遇到问题

目前我所拥有的代码可以用其他方法工作,除了我无法保存 Class AskDir()的实际自我"(即路径"/home/etc/")返回值

从我已阅读的内容中,我应该能够使用 a = AskDir(self)获得返回值 print(a),但是我得到的却是.!frame.!functionchangename.!askdir"

因此,简而言之,如何保存来自AskDir类内部函数的实际返回路径,并保存到MainWindow()类中的变量a中?

为了澄清,我想要的是MainWindow()类具有一个变量(a),该变量从Class SelectDir()内的子函数get()获取返回值,并且该值应为 path get()函数返回

简化代码:

class MainWindow:
    self.controller = controller

    # prints the button to this frame!
    getdir = AskDir(self)
    print(getdir) # this should return actual path (/home/etc/), but it doesnt

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

        self.selectdir = SelectDir(self, "Select directory", 
                                   "/home/user/folder/")

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

        self.act_dir = tk.StringVar()
        self.act_dir.set("/home/")

    def select_dir(self):
        self.selectdir.show()
        self.act_dir.set(self.selectdir.get())

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

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

    # THIS RETURNS THE ACTUAL PATH!
    def get(self):
        return self.selected

解决方案

这对您有用吗?

我认为没有必要将SelectDir作为类,因为它的用途只是调用askdirectory方法并返回选定的文件夹路径,因此我将其更改为函数.

在此示例中,按选择目录"按钮将打开目录对话框,然后将所选目录文本放入条目"小部件中.

如果MainWindow类希望在设置路径后对其执行任何操作,则只需调用AskDirget_dir方法,例如print(askDir.get_dir()

import tkinter as tk
from tkinter import filedialog

def doStuffFunction(value):
    print(value)

class MainWindow(tk.Frame):
    def __init__(self,master=None,**kw):
        tk.Frame.__init__(self,master=master,**kw)
        self.askDir = AskDir(self)
        self.askDir.grid()
        doStuffButton = tk.Button(self, text="Do Stuff", command=self.doStuff)
        doStuffButton.grid()


    def doStuff(self):
        doStuffFunction(self.askDir.get_dir())

class AskDir(tk.Frame):
    def __init__(self, master, **kw):
        tk.Frame.__init__(self, master=master,**kw)



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

        self.act_dir = tk.StringVar()
        self.act_dir.set("/home/")

        self.pathBox = tk.Entry(self, textvariable=self.act_dir,width=50)
        self.pathBox.grid(row=0,column=0)

    def select_dir(self):
        selectdir = SelectDir("Select directory", "/home/user/folder/")
        self.act_dir.set(selectdir)

    def get_dir(self):
        return self.act_dir.get()


def SelectDir(title,initial):
    result = filedialog.askdirectory(initialdir=initial,title=title)
    return result


if __name__ == '__main__':
    root = tk.Tk()
    MainWindow(root).grid()
    root.mainloop()

现在您可以在MainWindow中根据需要多次使用AskDir类(可能选择源"和目标"路径).这已通过MainWindow中的doStuff方法添加到我的示例中.

first of all im pretty new to OOP coding, so sorry for asking stupid questions.

I have a problem returning a value from Class AskDir that gets its value from Class SelectDir to Class MainWindow if that makes sense

Currently the code i have works, in every other way, except I cannot save the actual "self" (which is the path i.e. "/home/etc/") return value from Class AskDir() to another variable no matter what i do.

From what i have read i should be able to get the return value with a = AskDir(self) print(a), but what I get instead is ".!frame.!functionchangename.!askdir"

So in short, how to save actual return path from a function inside Class AskDir, to be saved to variable a in Class MainWindow()?

To clarify, what i want is for Class MainWindow() to have a variable (a) that gets the return value from a subfunction get() inside Class SelectDir(), and that value should be the path that get() function returns

simplified code:

class MainWindow:
    self.controller = controller

    # prints the button to this frame!
    getdir = AskDir(self)
    print(getdir) # this should return actual path (/home/etc/), but it doesnt

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

        self.selectdir = SelectDir(self, "Select directory", 
                                   "/home/user/folder/")

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

        self.act_dir = tk.StringVar()
        self.act_dir.set("/home/")

    def select_dir(self):
        self.selectdir.show()
        self.act_dir.set(self.selectdir.get())

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

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

    # THIS RETURNS THE ACTUAL PATH!
    def get(self):
        return self.selected

解决方案

Does this work for you?

I can see no need to have SelectDir as a class given that its purpose is just to call the askdirectory method and return the selected folder path so I've change it to a function.

With this example, pressing the "Select directory" button will open the directory dialog and then place the selected directory text in to an Entry widget.

If the MainWindow class wishes to do anything with the path once it has been set then you just need to call the get_dir method of AskDir e.g. print(askDir.get_dir()

import tkinter as tk
from tkinter import filedialog

def doStuffFunction(value):
    print(value)

class MainWindow(tk.Frame):
    def __init__(self,master=None,**kw):
        tk.Frame.__init__(self,master=master,**kw)
        self.askDir = AskDir(self)
        self.askDir.grid()
        doStuffButton = tk.Button(self, text="Do Stuff", command=self.doStuff)
        doStuffButton.grid()


    def doStuff(self):
        doStuffFunction(self.askDir.get_dir())

class AskDir(tk.Frame):
    def __init__(self, master, **kw):
        tk.Frame.__init__(self, master=master,**kw)



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

        self.act_dir = tk.StringVar()
        self.act_dir.set("/home/")

        self.pathBox = tk.Entry(self, textvariable=self.act_dir,width=50)
        self.pathBox.grid(row=0,column=0)

    def select_dir(self):
        selectdir = SelectDir("Select directory", "/home/user/folder/")
        self.act_dir.set(selectdir)

    def get_dir(self):
        return self.act_dir.get()


def SelectDir(title,initial):
    result = filedialog.askdirectory(initialdir=initial,title=title)
    return result


if __name__ == '__main__':
    root = tk.Tk()
    MainWindow(root).grid()
    root.mainloop()

You can now use the AskDir class as many times as you want in the MainWindow (perhaps to select Source and Destination paths). This has been added to my example with the doStuff method inside MainWindow.

这篇关于如何保存“自我"?当B类获得由C类返回的值时,将值从B类的变量返回到A类:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆