Understanding the Processing of Variables Defined by If in a Function

Asked 1 years ago, Updated 1 years ago, 304 views

Python is having trouble handling variables defined in if in the function.

We are creating the following programs:

目的:
Rename the png file in the same hierarchy folder or one lower hierarchy folder

  • Use the radio button on TKinter to switch the designated hierarchy to [same hierarchy] [one lower hierarchy]
  • Use widget variables to get folder paths
  • /*.png and /*/*.png are stuck together and retrieved in glob

I'm a beginner, so I've tried many things that came to mind, but I'm having trouble getting nothing.

The code in question
This path has probably not been obtained and is empty.
What should I do?
The radio button value acquisition seems to be working properly, but when I checked the filelist, there was nothing in it.

def list_create():
    def get_file_path():
        path = ''
        folder_path = folder.get()
        if rename_type.get() == 1: 
            path = folder_path + '/*.png'
        elif rename_type.get() == 2: 
            path = folder_path + '/*/*.png'
        return path
    
    path = get_file_path()
    filelist = glob.glob(path)

We have extracted the parts that may be related to the following.Folder and rename_type are available here.

#widget variable
folder = tkinter.StringVar()
rename_type = tk.StringVar(value="Rename File")

#frame
frame = tk.Frame(window)
frame.pack(side=tk.TOP, pady=10)
part_rename_radiobutton = tk.Radiobutton(frame, text="個々", variable=rename_type, value=1)
export_rename_radiobutton = tk.Radiobutton(frame, text="まとめて", variable=rename_type, value=2)

python python3

2023-03-30 12:53

1 Answers

rename_type = tk.StringVar(value="renaming")

Isn't this a string type?
The if statement branch does not apply because it is judged by numbers, and the contents of path are left empty

How about comparing strings and branching them or converting them into numbers and branching them?


2023-03-30 21:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.