Home>
I want to pass acquired information between methods

In python,Create multiple file paths selected from Explorerwas created.
At that time, in order to reuse the value obtained in the function in multiple functions, I knew that I would use a class.
However, since only a simple description method is understood, this process is difficult.

I don't understand even if this process is a problem to be solved by the class in the first place, but I would appreciate any advice.

What is the description of passing information using classes?

As an individual, I investigated classes and instances.
With the obtained information, I understand the flow ofspecifying arguments and passing the calculation results to other methods.
However, it is in a state of getting the element in the method and passing it in reverse.

I understand that the error code also points outargument, butIs there an element to specify from this side?

In the first place, there are parts before class instances.
It is a vague situation where the problem is, and I would appreciate it if you could give a word as an opportunity to review the learning part.

TypeError: start_btn () missing 1 required positional argument: 'search_field'
What it does

def btnRef Explorer start element acquisition
def btnStart Start operation after passing elements

Applicable source code

The argument specification is probably messed up.

# Judge when button is pressed
class btn_status:# Does init need to be listed?
    def __init __ (self, search_field):
        pass
    # [1/2] Get file path
    # Get multiple file paths from OS Explorer
    # Specify return value for use in other methods
    def get_file_path (self):
        fTyp = [('', '*')]
        iDir = os.path.abspath (os.path.dirname (__ file__))
        filepaths = filedialog.askopenfilenames (filetypes = fTyp, initialdir = iDir)
        # Acquired file path
        search_field = filePath.set (filepaths)
        # Specify file path: search_field as return value
        return self.search_field
    # [2/2] Display the acquired file name
    def start_btn (self):
        print (self.search_field)
h = btn_status ()
# Execute button
# btnStart = ttk.Button (frameBtn, text = 'Start', command = h.start_btn)# btnRef = ttk.Button (root, text = u'reference ', command = h.get_file_path)

Self part modification
Correction of description method itself

Append

■ Image of operation

I tried to work with the flow you gave me, but there was a point that I was concerned about in the processing part.

This is the part that calls btnRef_clicked () whenstart_btn ()is executed.
Naturally, the flow of delivery by return value issimilar to the behavior twice when viewed as a process.

I've come to the point of concern

1. If i use a class, can you get the elements of only return values ​​from other methods?

This time, I want a list of search_fields, and the file path acquisition operation is added.
I recognized it as aclass = variable/list sharing method.
In the first place, there is a possibility that the recognition has shifted.

2. If i have any questions about current issues, books or related websites, please let us know.

I'd like to reexamine my problem.
To collect vague information, it would be very helpful if there was an article in a related field.

Since we don't understand the terms well, we ’re sorry that we could n’t communicate well.

  • Answer # 1

    Can I pass lists and tuples without class?
    If it is a list, you can add or delete elements in the callee.

    def func1 ():
        pathes = ('/ tmp/a', '/ tmp/b', '/ tmp/c')
        return pathes
    def func2 (arg):
        print (arg)
    def main ():
        pathes = func1 ()
        func2 (pathes)
    if __name__ == '__main__':
        main ()

    An example using tk.StringVar () is shown below.

    import os
    import tkinter as tk
    import tkinter.filedialog as filedialog
    def mail_checker (root):
        root.title ('Mail checker')
        search_files = tk.StringVar ()
        tk.Label (root, text = "read mail and output necessary information"). pack ()
        frame1 = tk.Frame (root)
        frame1.pack ()
        tk.Label (frame1, text = "file name"). pack (side = 'left')
        tk.Entry (frame1, textvariable = search_files) .pack (side = 'left')
        tk.Button (frame1, text = 'reference', command = lambda: search_files.set (select_files ())). pack (side = 'left')
        frame2 = tk.Frame (root)
        frame2.pack ()
        tk.Button (frame2, text = 'Start', command = lambda: start (search_files.get ())). pack (side = 'left')
        tk.Button (frame2, text = 'Cancel', command = root.quit) .pack (side = 'left')
    def select_files ():
        fTyp = [('', '*')]
        iDir = os.path.abspath (os.path.dirname (__ file__))
        return filedialog.askopenfilenames (filetypes = fTyp, initialdir = iDir)
    def start (files):
        print (files)
    def main ():
        root = tk.Tk ()
        mail_checker (root)
        root.mainloop ()
    if __name__ == '__main__':
        main ()

Related articles