Tkinter,如何调整树视图缩进大小和指示器箭头图像 [英] Tkinter, how to adjust treeview indentation size and indicator arrow image

查看:26
本文介绍了Tkinter,如何调整树视图缩进大小和指示器箭头图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个树形视图小部件,您可以放大和缩小字体大小.问题是缩进在放大时与正常时不成比例.有没有办法调整缩进宽度?下面是两张图片,一张是正常的,另一张是放大的.我也想知道有没有办法改变指示器图像?

更新:由于下面的

解决方案

缩进

正如 jasonharper 和 Daniel Huckson 在评论中所说,缩进可以用

style.configure('Treeview', indent=100)

指示图像

可以通过创建自定义主题元素并使用它来替换 Treeview.Item 布局中的标准指示器来更改指示器图像.

这里的关键是要知道打开的项目('user1')和没有子项('user2')的状态的名称,关闭是默认状态.因此,打开指示器需要与处于状态 ('user1', '!user2') 的项目和空图像映射到处于状态 ('user2', ).

我使用 PIL 为指标创建了虚拟图像,但可以直接加载自定义图像.

from PIL import Image, ImageTk, ImageDraw将 tkinter 作为 tk 导入从 tkinter 导入 ttk根 = tk.Tk()风格 = ttk.Style(root)# 自定义指标图像im_open = Image.new('RGBA', (15, 15), '#00000000')im_empty = Image.new('RGBA', (15, 15), '#00000000')draw = ImageDraw.Draw(im_open)draw.polygon([(0, 4), (14, 4), (7, 11)], fill='yellow', outline='black')im_close= im_open.rotate(90)img_open = ImageTk.PhotoImage(im_open, name='img_open', master=root)img_close = ImageTk.PhotoImage(im_close, name='img_close', master=root)img_empty = ImageTk.PhotoImage(im_empty, name='img_empty', master=root)# 自定义指标style.element_create('Treeitem.myindicator','image', 'img_close', ('user1', '!user2', 'img_open'), ('user2', 'img_empty'),粘性=w",宽度=15)# 用自定义的替换 Treeitem.indicatorstyle.layout('Treeview.Item',[('Treeitem.padding',{'粘性':'nswe','儿童': [('Treeitem.myindicator', {'side': 'left', 'sticky': ''}),('Treeitem.image', {'side': 'left', 'sticky': ''}),('Treeitem.focus',{'边':'左','粘性':'','孩子': [('Treeitem.text', {'side': 'left', 'sticky': ''})]})]})])树 = ttk.Treeview(根)树.pack()tree.insert('', 'end', text='item 1', open=True)tree.insert('', 'end', text='item 2')tree.insert('I001', 'end', text='item 11', open=False)tree.insert('I001', 'end', text='item 12', open=False)tree.insert('I004', 'end', text='item 121', open=False)root.mainloop()

I have a treeview widget that you can zoom in and out the font size. The problem is the indentation is not proportional when zoomed in as to when it's normal. Is there a way to adjust the indentation width? Below are two images one is normal the other is zoomed. I also wonder if there is a way to change the indicator image?

UPDATE: I have solved the indentation problem thanks to jasonharper comments below. I still need to change the indicator arrow image can't seem to find any info on the web.

解决方案

Indentation

As said in the comments by jasonharper and Daniel Huckson, the indentation can be changed with

style.configure('Treeview', indent=100)

Indicator image

The indicator image can be changed by creating a custom theme element and using it in replacement of the standard indicator in the Treeview.Item layout.

The key point here is to know the names of the states of an opened item ('user1') and of an item without children ('user2'), closed being the default state. Therefore the open indicator needs to be mapped with items in the state ('user1', '!user2') and the empty image to the items in state ('user2', ).

I used PIL to create dummy images for the indicator, but one can directly load custom images instead.

from PIL import Image, ImageTk, ImageDraw
import tkinter as tk
from tkinter import ttk

root = tk.Tk()

style = ttk.Style(root)

# custom indicator images
im_open = Image.new('RGBA', (15, 15), '#00000000')
im_empty = Image.new('RGBA', (15, 15), '#00000000')
draw = ImageDraw.Draw(im_open)
draw.polygon([(0, 4), (14, 4), (7, 11)], fill='yellow', outline='black')
im_close= im_open.rotate(90)

img_open = ImageTk.PhotoImage(im_open, name='img_open', master=root)
img_close = ImageTk.PhotoImage(im_close, name='img_close', master=root)
img_empty = ImageTk.PhotoImage(im_empty, name='img_empty', master=root)

# custom indicator
style.element_create('Treeitem.myindicator',
                     'image', 'img_close', ('user1', '!user2', 'img_open'), ('user2', 'img_empty'),
                     sticky='w', width=15)
# replace Treeitem.indicator by custom one
style.layout('Treeview.Item',
[('Treeitem.padding',
  {'sticky': 'nswe',
   'children': [('Treeitem.myindicator', {'side': 'left', 'sticky': ''}),
    ('Treeitem.image', {'side': 'left', 'sticky': ''}),
    ('Treeitem.focus',
     {'side': 'left',
      'sticky': '',
      'children': [('Treeitem.text', {'side': 'left', 'sticky': ''})]})]})]
)


tree = ttk.Treeview(root)
tree.pack()
tree.insert('', 'end', text='item 1', open=True)
tree.insert('', 'end', text='item 2')
tree.insert('I001', 'end', text='item 11', open=False)
tree.insert('I001', 'end', text='item 12', open=False)
tree.insert('I004', 'end', text='item 121', open=False)

root.mainloop()

这篇关于Tkinter,如何调整树视图缩进大小和指示器箭头图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆