2017-12-04 27 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の独立性が損なわれます。拡大/縮小ボタンを動的にサイズに基づいて描かれているように見える、とthisminwidthオプションのデフォルトに応じて20で、私はそれが深さや画像を占めていることminwidthなどを計算する方法を記述しますWindows上

+0

はあなたがアイコンイメージを持つことを計画していますか? – Nae

+0

拡大/折りたたみボタンのウィンドウは、サイズに基づいて動的に描画され、[this](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-Treeview.html)に従って表示されます。 ) 'minwidth'オプションのデフォルトは20です。' minwidth'を計算する方法を書いて、深さと画像、テキストの幅+20を考慮します。 – Nae

答えて

1

this answer方法を使用して、言われていること+ 20

テキストの幅は、その正確な位置に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() 
関連する問題