简介:我需要在2个树对象之间创建一个并排的差异查看器,在其中我可以完全自定义每个项目的文本颜色和背景颜色。
Python版本:3.5.4
wxPython版本:4.0.3
结构:wx.Dialog,其中包含2个使用水平boxsizer派生的HyperTreeList。
代码(只有必需部分的剥离版本;树的大小始终与同步展开和折叠的相同):
import wx
import wx.lib.agw.hypertreelist as HTL
class AutoColumnSizeHTL(HTL.HyperTreeList):
def __init__(self,parent,agw_style,column_name):
super().__init__(parent,agwStyle=agw_style)
self.parent = parent
self.AddColumn(column_name)
self.SetColumnWidth(0,350)
self.main_window = self.GetMainWindow()
self.root = self.AddRoot("")
self.current_item = None
self.twin_tree = None
self.main_window.Bind(wx.EVT_SCROLLWIN,self.on_scroll_change)
def add_new_item(self,text,fg_color):
self.current_item = self.main_window.AppendItem(self.root,text)
self.main_window.SetItemTextColour(self.current_item,fg_color)
def add_new_item_child(self,fg_color,bg_color):
child_item = self.main_window.AppendItem(self.current_item,text)
self.main_window.SetItemTextColour(child_item,fg_color)
self.main_window.SetItemBackgroundColour(child_item,bg_color)
def set_twin_tree(self,twin):
self.twin_tree = twin
def OnSize(self,event):
row_width = self.parent.GetSize()[0] / 2
scrollbar_width = 21
self.SetColumnWidth(0,row_width-scrollbar_width)
super().OnSize(event)
def on_scroll_change(self,event):
scroll_units = self.main_window.GetScrollPos(wx.VERTICAL)
self.twin_tree.main_window.Scroll(-1,scroll_units)
event.Skip()
PS:我使用的是2个超树列表,每个列表有1列,而不是1个带有2列的超级树列表,因为我不知道如何更改第二列中项目的颜色。如果您碰巧知道该怎么做,那也可以解决这种麻烦。