2017-12-04 1 views
0

tkinter.TreeView에는 첫 번째 기본 열 (식별자 #0)이 있습니다. 예를 들어 이것은 나무의 '+'노래를 붙들기위한 것입니다.tkinter.TreeView의 열 '# 0'에 대한 자동 최소 너비

다른 열을 추가하면이 첫 번째 열의 크기가 조정되고 넓게 적용됩니다.

enter image description here

이 트 리뷰를 생산하는 코드입니다.

#!/usr/bin/env python3 
# -*- coding: utf-8 -*- 

from tkinter import * 
from tkinter import ttk 

root = Tk() 

tree = ttk.Treeview(root, columns=('one')) 
idx = tree.insert(parent='', index=END, values=('AAA')) 
tree.insert(parent=idx, index=END, values=('child')) 

tree.column('#0', stretch=False) # NO effect! 

tree.pack() 
root.mainloop() 

I은 ​​거기 + 부호에 따라 최소 고정 폭의 첫 번째 열 (#0)을 갖고 싶다. 요점은 데스크탑 환경과 사용자 설정이 다르므로 해당 열의 너비가 시스템마다 다릅니다. 그래서 여기에 픽셀로 고정 된 크기를 설정할 때 Python3과 Tkinter의 plattform 독립성이 깨질 것입니다. 확장 창에

+0

당신이 아이콘 이미지를 가진 계획이 있습니까? – Nae

+0

확장/축소 버튼이있는 창에서 크기에 따라 동적으로 그려진 것으로 보이며 [this] (http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-Treeview.html)에 따르면)'minwidth' 옵션의 기본값은 20입니다.'minwidth'를 계산하여 깊이와 이미지, 텍스트 폭 +20을 계산하는 방법을 씁니다. – Nae

답변

1

이/축소 버튼을 동적으로 크기에 따라 그려 질 것, 그리고 20 thisminwidth 옵션 기본값에 따라 내가 계산하는 방법을 써서 minwidth는 깊이와 이미지를 차지하도록하고 텍스트 폭 + 20 this answer에게 방법을 이용하여, 상기 되 그게

그 정확한 위치에서 tagbind을 파괴함으로써, 상기 minwidth Tk의 기본값에 열 너비를 고정하기위한 기록 될 수

#the minimum width default that Tk assigns 
minwidth = tree.column('#0', option='minwidth') 

tree.column('#0', width=minwidth) 

#disabling resizing for '#0' column particularly 
def handle_click(event): 
    if tree.identify_region(event.x, event.y) == "separator": 
     if tree.identify_column(event.x) == '#0': 
      return "break" 

#to have drag drop to have no effect 
tree.bind('<Button-1>', handle_click) 
#further disabling the double edged arrow display 
tree.bind('<Motion>', handle_click) 

그리고 완전한 예 :

#!/usr/bin/env python3 
# -*- coding: utf-8 -*- 

from tkinter import * 
from tkinter import ttk 

root = Tk() 

tree = ttk.Treeview(root, columns=('one')) 
idx = tree.insert(parent='', index=END, values=('AAA')) 
tree.insert(parent=idx, index=END, values=('child')) 

tree.column('#0', stretch=False) # NO effect! 

#the minimum width default that Tk assigns 
minwidth = tree.column('#0', option='minwidth') 

tree.column('#0', width=minwidth) 

#disabling resizing for '#0' column particularly 
def handle_click(event): 
    if tree.identify_region(event.x, event.y) == "separator": 
     if tree.identify_column(event.x) == '#0': 
      return "break" 

#to have drag drop to have no effect 
tree.bind('<Button-1>', handle_click) 
#further disabling the double edged arrow display 
tree.bind('<Motion>', handle_click) 

tree.pack() 
root.mainloop()