본문 바로가기

개발자 모드/파이썬(python)

파이썬 treeview 2개 불러서 왼쪽 선택시 오른쪽에 표시

728x90
import tkinter as tk  # 툴킷 인터페이스
import tkinter.ttk as ttk  # tk의 확장 (트리뷰, 콤보박스 등 제공)

root = tk.Tk()
 
frame_1 = ttk.LabelFrame(root, text="Raw Data")
frame_1.pack(side = "left")

frame_2 = ttk.LabelFrame(root, text="Analyzed Data")
frame_2.pack(side = "left")

scrollbar1 = ttk.Scrollbar(frame_1)
# scrollbar1.pack(side = "right", fill = "y", expand = False)
scrollbar1.pack(side = "right", fill = "both", expand = True)

scrollbar2 = ttk.Scrollbar(frame_2)
scrollbar2.pack(side = "right", fill = "y", expand = True)

# scrollbar1 = ttk.Scrollbar(frame_1)
# scrollbar2 = ttk.Scrollbar(frame_2)

tree = ttk.Treeview(frame_1, columns=(1, 2, 3), height=5, show="headings",  yscrollcommand = scrollbar1.set)
tree.pack(side='left')
scrollbar1.config(command = tree.yview)





# 필드명
tree.heading(1, text="A")
tree.heading(2, text="B")
tree.heading(3, text="C")

# 기본 너비
tree.column(1, width=100)
tree.column(2, width=100)
tree.column(3, width=100)

tree2 = ttk.Treeview(frame_2, columns=(1, 2, 3), height=5, show="headings", yscrollcommand = scrollbar2.set)
tree2.pack(side='left')
scrollbar2.config(command = tree2.yview)

# 필드명
tree2.heading(1, text="one")
tree2.heading(2, text="two")
tree2.heading(3, text="three")

# 기본 너비
tree2.column(1, width=100)
tree2.column(2, width=100)
tree2.column(3, width=100)


# 기본 데이터 추가
data = [["1", "2", "3"],
        ["4", "5", "6"],
        ["7", "8", "9"],
        ["10", "11", "12"],
        ["13", "14", "15"],
        ["13", "14", "15"],
        ["13", "14", "15"],
        ["13", "14", "15"],
        ["13", "14", "15"],
        ["16", "17", "18"]]

for val in data:
    tree.insert('', 'end', values=(val[0], val[1], val[2]))
    print(val[0], val[1], val[2])
  

def click_item(event):
    selectedItem = tree.focus()
    # getValue = tree.item(selectedItem).get('values')  # 딕셔너리의 값만 가져오기
    # label.configure(text=getValue)  # 라벨 내용 바꾸기
    getValue = tree.item(selectedItem).get('values')  # 딕셔너리의 값만 가져오기
    
    print(getValue)
    

    print(getValue[0], getValue[1], getValue[2])
    
    tree2.insert("",'end',values = (getValue[0], getValue[1], getValue[2]))
    
    
    tree2.yview_moveto(1)
    
    # getValue= list(map(str, getValue))
    # getValue= list(getValue)
    # print(val[0], val[1], val[2])
    # print(getValue)    
    # print(type(val))
    # print(type(getValue))
    
    
   
    


tree.bind('<ButtonRelease-1>', click_item)
root.mainloop()

 

 

 

 

728x90