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
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
■ 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.
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.
An example using tk.StringVar () is shown below.