WxPython演示代码-传递记录器对象
我正在我的机器上尝试一些WXpython演示代码。。。只是想了解如何传递logger对象…它应该只是logger对象,还是可以传递一个文件名来写入文件 这是我修改过的代码WxPython演示代码-传递记录器对象,wxpython,Wxpython,我正在我的机器上尝试一些WXpython演示代码。。。只是想了解如何传递logger对象…它应该只是logger对象,还是可以传递一个文件名来写入文件 这是我修改过的代码 import wx, wx.lib.customtreectrl, wx.gizmos try: import treemixin except ImportError: from wx.lib.mixins import treemixin overview = treemixin.__doc__ cl
import wx, wx.lib.customtreectrl, wx.gizmos
try:
import treemixin
except ImportError:
from wx.lib.mixins import treemixin
overview = treemixin.__doc__
class TreeModel(object):
''' TreeModel holds the domain objects that are shown in the different
tree controls. Each domain object is simply a two-tuple consisting of
a label and a list of child tuples, i.e. (label, [list of child tuples]).
'''
def __init__(self, *args, **kwargs):
self.items = []
self.itemCounter = 0
super(TreeModel, self).__init__(*args, **kwargs)
def GetItem(self, indices):
text, children = 'Hidden root', self.items
for index in indices:
text, children = children[index]
return text, children
def GetText(self, indices):
return self.GetItem(indices)[0]
def GetChildren(self, indices):
return self.GetItem(indices)[1]
def GetChildrenCount(self, indices):
return len(self.GetChildren(indices))
def SetChildrenCount(self, indices, count):
children = self.GetChildren(indices)
while len(children) > count:
children.pop()
while len(children) < count:
children.append(('item %d'%self.itemCounter, []))
self.itemCounter += 1
def MoveItem(self, itemToMoveIndex, newParentIndex):
itemToMove = self.GetItem(itemToMoveIndex)
newParentChildren = self.GetChildren(newParentIndex)
newParentChildren.append(itemToMove)
oldParentChildren = self.GetChildren(itemToMoveIndex[:-1])
oldParentChildren.remove(itemToMove)
class DemoTreeMixin(treemixin.VirtualTree, treemixin.DragAndDrop,
treemixin.ExpansionState):
def __init__(self, *args, **kwargs):
self.model = kwargs.pop('treemodel')
# self.log = kwargs.pop('log')
super(DemoTreeMixin, self).__init__(*args, **kwargs)
self.CreateImageList()
def CreateImageList(self):
size = (16, 16)
self.imageList = wx.ImageList(*size)
for art in wx.ART_FOLDER, wx.ART_FILE_OPEN, wx.ART_NORMAL_FILE:
self.imageList.Add(wx.ArtProvider.GetBitmap(art, wx.ART_OTHER,
size))
self.AssignImageList(self.imageList)
def OnGetItemText(self, indices):
return self.model.GetText(indices)
def OnGetChildrenCount(self, indices):
return self.model.GetChildrenCount(indices)
def OnGetItemFont(self, indices):
# Show how to change the item font. Here we use a small font for
# items that have children and the default font otherwise.
if self.model.GetChildrenCount(indices) > 0:
return wx.SMALL_FONT
else:
return super(DemoTreeMixin, self).OnGetItemFont(indices)
def OnGetItemTextColour(self, indices):
# Show how to change the item text colour. In this case second level
# items are coloured red and third level items are blue. All other
# items have the default text colour.
if len(indices) % 2 == 0:
return wx.RED
elif len(indices) % 3 == 0:
return wx.BLUE
else:
return super(DemoTreeMixin, self).OnGetItemTextColour(indices)
def OnGetItemBackgroundColour(self, indices):
# Show how to change the item background colour. In this case the
# background colour of each third item is green.
if indices[-1] == 2:
return wx.GREEN
else:
return super(DemoTreeMixin,
self).OnGetItemBackgroundColour(indices)
def OnGetItemImage(self, indices, which):
# Return the right icon depending on whether the item has children.
if which in [wx.TreeItemIcon_Normal, wx.TreeItemIcon_Selected]:
if self.model.GetChildrenCount(indices):
return 0
else:
return 2
else:
return 1
def OnDrop(self, dropTarget, dragItem):
dropIndex = self.GetIndexOfItem(dropTarget)
dropText = self.model.GetText(dropIndex)
dragIndex = self.GetIndexOfItem(dragItem)
dragText = self.model.GetText(dragIndex)
# self.log.write('drop %s %s on %s %s'%(dragText, dragIndex,
# dropText, dropIndex))
self.model.MoveItem(dragIndex, dropIndex)
self.GetParent().RefreshItems()
class VirtualTreeCtrl(DemoTreeMixin, wx.TreeCtrl):
pass
class VirtualTreeListCtrl(DemoTreeMixin, wx.gizmos.TreeListCtrl):
def __init__(self, *args, **kwargs):
kwargs['style'] = wx.TR_DEFAULT_STYLE | wx.TR_FULL_ROW_HIGHLIGHT
super(VirtualTreeListCtrl, self).__init__(*args, **kwargs)
self.AddColumn('Column 0')
self.AddColumn('Column 1')
for art in wx.ART_TIP, wx.ART_WARNING:
self.imageList.Add(wx.ArtProvider.GetBitmap(art, wx.ART_OTHER,
(16, 16)))
def OnGetItemText(self, indices, column=0):
# Return a different label depending on column.
return '%s, column %d'%\
(super(VirtualTreeListCtrl, self).OnGetItemText(indices), column)
def OnGetItemImage(self, indices, which, column=0):
# Also change the image of the other columns when the item has
# children.
if column == 0:
return super(VirtualTreeListCtrl, self).OnGetItemImage(indices,
which)
elif self.OnGetChildrenCount(indices):
return 4
else:
return 3
class VirtualCustomTreeCtrl(DemoTreeMixin,
wx.lib.customtreectrl.CustomTreeCtrl):
def __init__(self, *args, **kwargs):
self.checked = {}
kwargs['style'] = wx.TR_HIDE_ROOT | \
wx.TR_HAS_BUTTONS | wx.TR_FULL_ROW_HIGHLIGHT
super(VirtualCustomTreeCtrl, self).__init__(*args, **kwargs)
self.Bind(wx.lib.customtreectrl.EVT_TREE_ITEM_CHECKED,
self.OnItemChecked)
def OnGetItemType(self, indices):
if len(indices) == 1:
return 1
elif len(indices) == 2:
return 2
else:
return 0
def OnGetItemChecked(self, indices):
return self.checked.get(indices, False)
def OnItemChecked(self, event):
item = event.GetItem()
itemIndex = self.GetIndexOfItem(item)
if self.GetItemType(item) == 2:
# It's a radio item; reset other items on the same level
for nr in range(self.GetChildrenCount(self.GetItemParent(item))):
self.checked[itemIndex[:-1]+(nr,)] = False
self.checked[itemIndex] = True
class TreeNotebook(wx.Notebook):
def __init__(self, *args, **kwargs):
treemodel = kwargs.pop('treemodel')
# log = kwargs.pop('log')
super(TreeNotebook, self).__init__(*args, **kwargs)
self.trees = []
for class_, title in [(VirtualTreeCtrl, 'TreeCtrl'),
(VirtualTreeListCtrl, 'TreeListCtrl'),
(VirtualCustomTreeCtrl, 'CustomTreeCtrl')]:
# tree = class_(self, treemodel=treemodel, log=log)
tree = class_(self, treemodel=treemodel)
self.trees.append(tree)
self.AddPage(tree, title)
self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged)
def OnPageChanged(self, event):
oldTree = self.GetPage(event.OldSelection)
newTree = self.GetPage(event.Selection)
newTree.RefreshItems()
newTree.SetExpansionState(oldTree.GetExpansionState())
event.Skip()
def GetIndicesOfSelectedItems(self):
tree = self.trees[self.GetSelection()]
if tree.GetSelections():
return [tree.GetIndexOfItem(item) for item in tree.GetSelections()]
else:
return [()]
def RefreshItems(self):
tree = self.trees[self.GetSelection()]
tree.RefreshItems()
tree.UnselectAll()
class TestPanel(wx.App):
# def __init__(self, parent, log):
# self.log = log
# super(TestPanel, self).__init__(parent)
def __init__(self, redirect=False, filename=None):
wx.App.__init__(self, redirect, filename)
self.frame = wx.Frame(None, wx.ID_ANY, title='My Title')
# self.log = log
self.treemodel = TreeModel()
self.CreateControls()
self.LayoutControls()
def CreateControls(self):
self.notebook = TreeNotebook(self, treemodel=self.treemodel)
#log=self.log)
self.label = wx.StaticText(self, label='Number of children: ')
self.childrenCountCtrl = wx.SpinCtrl(self, value='0', max=10000)
self.button = wx.Button(self, label='Update children')
self.button.Bind(wx.EVT_BUTTON, self.OnEnter)
def LayoutControls(self):
hSizer = wx.BoxSizer(wx.HORIZONTAL)
options = dict(flag=wx.ALIGN_CENTER_VERTICAL|wx.ALL, border=2)
hSizer.Add(self.label, **options)
hSizer.Add(self.childrenCountCtrl, 2, **options)
hSizer.Add(self.button, **options)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.notebook, 1, wx.EXPAND)
sizer.Add(hSizer, 0, wx.EXPAND)
self.SetSizer(sizer)
def OnEnter(self, event):
indicesList = self.notebook.GetIndicesOfSelectedItems()
newChildrenCount = self.childrenCountCtrl.GetValue()
for indices in indicesList:
text = self.treemodel.GetText(indices)
oldChildrenCount = self.treemodel.GetChildrenCount(indices)
# self.log.write('%s %s now has %d children (was %d)'%(text, indices,
# newChildrenCount, oldChildrenCount))
self.treemodel.SetChildrenCount(indices, newChildrenCount)
self.notebook.RefreshItems()
# def runTest(frame, nb, log):
# win = TestPanel(nb, log)
# return win
#
#
# if __name__ == '__main__':
# import sys, os, run
# run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
if __name__ == '__main__':
app = TestPanel()
app.MainLoop()
导入wx,wx.lib.customtreectrl,wx.gizmos
尝试:
进口树霉素
除恐怖外:
从wx.lib.mixin导入treemixin
概述=treemixin.\uu文件__
类树模型(对象):
''TreeModel保存在不同视图中显示的域对象
树控件。每个域对象只是一个两元组,由
标签和子元组列表,即(标签,[子元组列表])。
'''
定义初始化(self,*args,**kwargs):
self.items=[]
self.itemCounter=0
super(树模型,self)。\uuuuu初始化(*args,**kwargs)
def GetItem(自身,索引):
text,children='Hidden root',self.items
对于索引中的索引:
文本,子项=子项[索引]
返回文本,子项
def GetText(自我,索引):
返回self.GetItem(索引)[0]
def GetChildren(自我,索引):
返回self.GetItem(索引)[1]
def GetChildrenCount(自身,索引):
返回len(self.GetChildren(索引))
def SetChildrenCount(自身、索引、计数):
children=self.GetChildren(索引)
而len(儿童)>计数:
children.pop()
而len(儿童)<计数:
附加(('item%d'%self.itemCounter,[]))
self.itemCounter+=1
def MoveItem(self、itemToMoveIndex、newParentIndex):
itemToMove=self.GetItem(itemToMoveIndex)
newParentChildren=self.GetChildren(newParentIndex)
newParentChildren.append(itemToMove)
oldParentChildren=self.GetChildren(itemToMoveIndex[:-1])
oldParentChildren.remove(itemToMove)
DemoReemixin类(treemixin.VirtualTree,treemixin.dragandrop,
treemixin.ExpansionState):
定义初始化(self,*args,**kwargs):
self.model=kwargs.pop('treemodel'))
#self.log=kwargs.pop('log'))
super(demoReemixin,self)。\uuuuuu初始值(*args,**kwargs)
self.CreateImageList()
def CreateImageList(自身):
大小=(16,16)
self.imageList=wx.imageList(*大小)
对于wx.art_文件夹中的艺术,wx.art_文件打开,wx.art_普通文件:
self.imageList.Add(wx.ArtProvider.GetBitmap(art,wx.art_OTHER,
尺寸)
self.AssignImageList(self.imageList)
def OnGetItemText(自身,索引):
返回self.model.GetText(索引)
def OnGetChildrenCount(自我,索引):
返回self.model.GetChildrenCount(索引)
def OnGetItemFont(自身,索引):
#显示如何更改项目字体。在这里,我们使用小字体
#具有子项的项目,否则使用默认字体。
如果self.model.GetChildrenCount(索引)>0:
返回wx.SMALL\u字体
其他:
返回super(demoReemixin,self).OnGetItemFont(索引)
def OnGetItemTextColor(自身、索引):
#演示如何更改项目文本颜色。在本例中为第二级
#物品颜色为红色,三级物品颜色为蓝色。所有其他
#项目具有默认的文本颜色。
如果len(索引)%2==0:
返回wx.RED
elif len(指数)%3==0:
返回wx.BLUE
其他:
返回super(demoReemixin,self).onGetItemTextColor(索引)
def OnGetItemBackgroundColor(自身、索引):
#演示如何更改项目背景颜色。在这种情况下
#第三项的背景色为绿色。
如果索引[-1]==2:
返回wx.GREEN
其他:
返回super(mixin,
自)OnGetItemBackgroundColor(索引)
def OnGetItemImage(自身、索引,其中):
#根据项目是否有子项返回右图标。
如果[wx.TreeItemIcon_正常,wx.TreeItemIcon_已选择]:
如果self.model.GetChildrenCount(索引):
返回0
其他:
返回2
其他:
返回1
def OnDrop(自身、dropTarget、dragItem):
dropIndex=self.GetIndexOfItem(dropTarget)
dropText=self.model.GetText(dropIndex)
dragIndex=self.GetIndexOfItem(dragItem)
dragText=self.model.GetText(dragIndex)
#self.log.write('将%s%s放在%s%s%%上(dragText、dragIndex、,
#dropText,dropIndex)
self.model.MoveItem(dragIndex、dropIndex)
self.GetParent().RefreshItems()
类VirtualTreeCtrl(DemoReeMixin,wx.TreeCtrl):
通过
类VirtualTreeListCtrl(DemoReemixin,wx.gizmos.TreeListCtrl):
定义初始化(self,*args,**kwargs):
kwargs['style']=wx.TR_默认_样式| wx.TR_完整_行_高亮显示
super(VirtualTreeListCtrl,self)。\uuuuu init\uuuuu(*args,**kwargs)
self.AddColumn('Column 0')
self.AddColumn('Column 1')
对于wx.art_提示中的艺术,wx.art_警告:
self.imageList.Add(wx.ArtProvider.GetBitmap(art,wx.art_OTHER,
(16, 16)))
def OnGetItemText(自身,索引,列=0):
#根据列返回不同的标签。
返回“%s”,列%d'%\
(super(VirtualTreeListCtrl,self).OnGetItemText(索引),列)
def OnGetItemImage(自身,索引,其中,列=0):
#还可以在项目已完成时更改其他列的图像
#孩子们。
如果列==0:
返回super(VirtualTreeListCtrl,self).OnGetItemImage(索引,
(其中)
elif self.OnGetChildrenCount(索引):
返回4
其他:
返回3
类VirtualCustomTreeCtrl(DemoReeMixin,
wx.lib.c
import wx, wx.lib.customtreectrl, wx.gizmos
try:
import treemixin
except ImportError:
from wx.lib.mixins import treemixin
overview = treemixin.__doc__
class TreeModel(object):
''' TreeModel holds the domain objects that are shown in the different
tree controls. Each domain object is simply a two-tuple consisting of
a label and a list of child tuples, i.e. (label, [list of child tuples]).
'''
def __init__(self, *args, **kwargs):
self.items = []
self.itemCounter = 0
super(TreeModel, self).__init__(*args, **kwargs)
def GetItem(self, indices):
text, children = 'Hidden root', self.items
for index in indices:
text, children = children[index]
return text, children
def GetText(self, indices):
return self.GetItem(indices)[0]
def GetChildren(self, indices):
return self.GetItem(indices)[1]
def GetChildrenCount(self, indices):
return len(self.GetChildren(indices))
def SetChildrenCount(self, indices, count):
children = self.GetChildren(indices)
while len(children) > count:
children.pop()
while len(children) < count:
children.append(('item %d'%self.itemCounter, []))
self.itemCounter += 1
def MoveItem(self, itemToMoveIndex, newParentIndex):
itemToMove = self.GetItem(itemToMoveIndex)
newParentChildren = self.GetChildren(newParentIndex)
newParentChildren.append(itemToMove)
oldParentChildren = self.GetChildren(itemToMoveIndex[:-1])
oldParentChildren.remove(itemToMove)
class DemoTreeMixin(treemixin.VirtualTree, treemixin.DragAndDrop,
treemixin.ExpansionState):
def __init__(self, *args, **kwargs):
self.model = kwargs.pop('treemodel')
# self.log = kwargs.pop('log')
super(DemoTreeMixin, self).__init__(*args, **kwargs)
self.CreateImageList()
def CreateImageList(self):
size = (16, 16)
self.imageList = wx.ImageList(*size)
for art in wx.ART_FOLDER, wx.ART_FILE_OPEN, wx.ART_NORMAL_FILE:
self.imageList.Add(wx.ArtProvider.GetBitmap(art, wx.ART_OTHER,
size))
self.AssignImageList(self.imageList)
def OnGetItemText(self, indices):
return self.model.GetText(indices)
def OnGetChildrenCount(self, indices):
return self.model.GetChildrenCount(indices)
def OnGetItemFont(self, indices):
# Show how to change the item font. Here we use a small font for
# items that have children and the default font otherwise.
if self.model.GetChildrenCount(indices) > 0:
return wx.SMALL_FONT
else:
return super(DemoTreeMixin, self).OnGetItemFont(indices)
def OnGetItemTextColour(self, indices):
# Show how to change the item text colour. In this case second level
# items are coloured red and third level items are blue. All other
# items have the default text colour.
if len(indices) % 2 == 0:
return wx.RED
elif len(indices) % 3 == 0:
return wx.BLUE
else:
return super(DemoTreeMixin, self).OnGetItemTextColour(indices)
def OnGetItemBackgroundColour(self, indices):
# Show how to change the item background colour. In this case the
# background colour of each third item is green.
if indices[-1] == 2:
return wx.GREEN
else:
return super(DemoTreeMixin,
self).OnGetItemBackgroundColour(indices)
def OnGetItemImage(self, indices, which):
# Return the right icon depending on whether the item has children.
if which in [wx.TreeItemIcon_Normal, wx.TreeItemIcon_Selected]:
if self.model.GetChildrenCount(indices):
return 0
else:
return 2
else:
return 1
def OnDrop(self, dropTarget, dragItem):
dropIndex = self.GetIndexOfItem(dropTarget)
dropText = self.model.GetText(dropIndex)
dragIndex = self.GetIndexOfItem(dragItem)
dragText = self.model.GetText(dragIndex)
self.model.MoveItem(dragIndex, dropIndex)
self.GetParent().RefreshItems()
class VirtualTreeCtrl(DemoTreeMixin, wx.TreeCtrl):
pass
class VirtualTreeListCtrl(DemoTreeMixin, wx.gizmos.TreeListCtrl):
def __init__(self, *args, **kwargs):
kwargs['style'] = wx.TR_DEFAULT_STYLE | wx.TR_FULL_ROW_HIGHLIGHT
super(VirtualTreeListCtrl, self).__init__(*args, **kwargs)
self.AddColumn('Column 0')
self.AddColumn('Column 1')
for art in wx.ART_TIP, wx.ART_WARNING:
self.imageList.Add(wx.ArtProvider.GetBitmap(art, wx.ART_OTHER,
(16, 16)))
def OnGetItemText(self, indices, column=0):
# Return a different label depending on column.
return '%s, column %d'%\
(super(VirtualTreeListCtrl, self).OnGetItemText(indices), column)
def OnGetItemImage(self, indices, which, column=0):
# Also change the image of the other columns when the item has
# children.
if column == 0:
return super(VirtualTreeListCtrl, self).OnGetItemImage(indices,
which)
elif self.OnGetChildrenCount(indices):
return 4
else:
return 3
class VirtualCustomTreeCtrl(DemoTreeMixin,
wx.lib.customtreectrl.CustomTreeCtrl):
def __init__(self, *args, **kwargs):
self.checked = {}
kwargs['style'] = wx.TR_HIDE_ROOT | \
wx.TR_HAS_BUTTONS | wx.TR_FULL_ROW_HIGHLIGHT
super(VirtualCustomTreeCtrl, self).__init__(*args, **kwargs)
self.Bind(wx.lib.customtreectrl.EVT_TREE_ITEM_CHECKED,
self.OnItemChecked)
def OnGetItemType(self, indices):
if len(indices) == 1:
return 1
elif len(indices) == 2:
return 2
else:
return 0
def OnGetItemChecked(self, indices):
return self.checked.get(indices, False)
def OnItemChecked(self, event):
item = event.GetItem()
itemIndex = self.GetIndexOfItem(item)
if self.GetItemType(item) == 2:
# It's a radio item; reset other items on the same level
for nr in range(self.GetChildrenCount(self.GetItemParent(item))):
self.checked[itemIndex[:-1]+(nr,)] = False
self.checked[itemIndex] = True
class TreeNotebook(wx.Notebook):
def __init__(self, *args, **kwargs):
treemodel = kwargs.pop('treemodel')
super(TreeNotebook, self).__init__(*args, **kwargs)
self.trees = []
for class_, title in [(VirtualTreeCtrl, 'TreeCtrl'),
(VirtualTreeListCtrl, 'TreeListCtrl'),
(VirtualCustomTreeCtrl, 'CustomTreeCtrl')]:
tree = class_(self, treemodel=treemodel)
self.trees.append(tree)
self.AddPage(tree, title)
self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged)
def OnPageChanged(self, event):
oldTree = self.GetPage(event.OldSelection)
newTree = self.GetPage(event.Selection)
newTree.RefreshItems()
newTree.SetExpansionState(oldTree.GetExpansionState())
event.Skip()
def GetIndicesOfSelectedItems(self):
tree = self.trees[self.GetSelection()]
if tree.GetSelections():
return [tree.GetIndexOfItem(item) for item in tree.GetSelections()]
else:
return [()]
def RefreshItems(self):
tree = self.trees[self.GetSelection()]
tree.RefreshItems()
tree.UnselectAll()
class TestPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.treemodel = TreeModel()
self.CreateControls()
self.LayoutControls()
def CreateControls(self):
self.notebook = TreeNotebook(self, treemodel=self.treemodel)
self.label = wx.StaticText(self, label='Number of children: ')
self.childrenCountCtrl = wx.SpinCtrl(self, value='0', max=10000)
self.button = wx.Button(self, label='Update children')
self.button.Bind(wx.EVT_BUTTON, self.OnEnter)
def LayoutControls(self):
hSizer = wx.BoxSizer(wx.HORIZONTAL)
options = dict(flag=wx.ALIGN_CENTER_VERTICAL|wx.ALL, border=2)
hSizer.Add(self.label, **options)
hSizer.Add(self.childrenCountCtrl, 2, **options)
hSizer.Add(self.button, **options)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.notebook, 1, wx.EXPAND)
sizer.Add(hSizer, 0, wx.EXPAND)
self.SetSizer(sizer)
def OnEnter(self, event):
indicesList = self.notebook.GetIndicesOfSelectedItems()
newChildrenCount = self.childrenCountCtrl.GetValue()
for indices in indicesList:
text = self.treemodel.GetText(indices)
oldChildrenCount = self.treemodel.GetChildrenCount(indices)
# self.log.write('%s %s now has %d children (was %d)'%(text, indices,
# newChildrenCount, oldChildrenCount))
self.treemodel.SetChildrenCount(indices, newChildrenCount)
self.notebook.RefreshItems()
class MainFrame(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
""""""
wx.Frame.__init__(self, None, title="Test")
panel = TestPanel(self)
self.Show()
if __name__ == '__main__':
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()