combbobox의 값에 따라서 dynamic하게 row를 생성합니다.
생성된 row에서 선택된 파일들이 각각 다르게 사용될 수 있기 때문에 button, label, entry는 별도로 생성합니다.
import tkinter
import tkinter.ttk
from tkinter import filedialog
main_category=[
"Car",
"Animal"
]
window = tkinter.Tk()
window.title("tkinter Test")
window.geometry()
window.resizable(0,0)
row_order=2
title_column=0
label_column=1
button_column=2
def get_category(eventObject):
if category_combobox.get()=="Car":
car_button["state"] = "normal"
car_path.set("")
car_title.grid(column=title_column, row=row_order, sticky='w')
car_label.grid(column=label_column, row=row_order)
car_button.grid(column=button_column, row=row_order)
elif category_combobox.get()=="Animal":
animal_button["state"] = "normal"
animal_path.set("")
animal_title.grid(column=title_column, row=row_order, sticky='w')
animal_label.grid(column=label_column, row=row_order)
animal_button.grid(column=button_column, row=row_order)
def select_car():
input_path = filedialog.askopenfilename()
car_path.set(input_path)
def select_animal():
input_path = filedialog.askopenfilename()
animal_path.set(input_path)
category_title=tkinter.Label(window, text="Main Category", font=("Arial", 10, "bold"))
category_title.grid(column=0, row=0, sticky='w') # grid return None always
category_combobox=tkinter.ttk.Combobox(window, height=len(main_category), value=main_category)
category_combobox.set("Select main Category")
category_combobox.grid(column=0, row=1, sticky='w')
# combobox에 <<ComboboxSelected>> event를 bind하여 선택된 combobox 값에 따라 동작하는 함수를 생성합니다.
category_combobox.bind("<<ComboboxSelected>>", get_category) # event, triggerd function
"""
grid_remove()를 사용해서 임시로 grid를 삭제하고, combobox에 bind된 evnet를 통해 grid 값을 재설정하여
동적으로 row가 등장하도록 합니다.
"""
car_path = tkinter.StringVar()
car_title = tkinter.ttk.Label(window, text="Select car")
car_title.grid_remove()
car_label = tkinter.ttk.Entry(window, textvariable=car_path)
car_label.grid_remove()
car_button = tkinter.ttk.Button(window, text="Browse", command=select_car)
car_button.grid_remove()
animal_path = tkinter.StringVar()
animal_title = tkinter.ttk.Label(window, text="Select animal")
animal_title.grid_remove()
animal_label = tkinter.ttk.Entry(window, textvariable=animal_path)
animal_label.grid_remove()
animal_button = tkinter.ttk.Button(window, text="Browse", command=select_animal)
animal_button.grid_remove()
window.mainloop()