Tuesday, November 20, 2007

wxPython - TreeControl - SplitPanel


# -*- coding: iso-8859-1 -*-

# Référence : wxPython in Action from Robin Dunn - Noel Rappin


import os
import wx

ID_ABOUT = wx.NewId()
ID_OPEN = wx.NewId()
ID_EXIT = wx.NewId()


tree = [
['Dakar',
['Administration',
['Supervision',
['Disques','Banques','File d''attentes']],
'Réseaux'],
], #/Dakar
['Denver',
['Administration','Supervision','Réseaux'],
] #/Denver
]

class TreeServeur(wx.TreeCtrl):
def __init__(self, parent,id):
super(TreeServeur, self).__init__(parent,id)
self.Bind(wx.EVT_TREE_ITEM_EXPANDING, self.OnExpandItem)
self.Bind(wx.EVT_TREE_ITEM_COLLAPSING, self.OnCollapseItem)
self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated)

root = self.AddRoot("Serveurs")
self.AddTreeNodes(root,tree)

def AddTreeNodes(self, parentItem, items):
for item in items:
if type(item) == str:
self.AppendItem(parentItem, item)
else:
newItem = self.AppendItem(parentItem, item[0])
self.AddTreeNodes(newItem, item[1])

def OnExpandItem(self, evt):
pass

def OnCollapseItem(self,evt):
pass

def OnActivated(self,evt):
print "OnActivated: ", self.GetItemText(evt.GetItem())


class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(1024, 800))

self.initpos = 50

#Status Bar
self.CreateStatusBar()

#Menu Fichier
filemenu = wx.Menu()
filemenu.Append(ID_OPEN,'&Ouvrir')
filemenu.Append(ID_EXIT,'&Quitter')
#Menu Aide
aidemenu = wx.Menu()
aidemenu.Append(ID_ABOUT,'&Aide')
#Barre de Menu
menubar = wx.MenuBar()
menubar.Append(filemenu,'&Fichier')
menubar.Append(aidemenu,'&Aide')
self.SetMenuBar(menubar)
#Gestionnaire d'evenement menu
wx.EVT_MENU(self, ID_ABOUT, self.OnAbout)
wx.EVT_MENU(self, ID_EXIT, self.OnExit)
wx.EVT_MENU(self, ID_OPEN, self.OnOpen)

#Splitter
splitter1 = wx.SplitterWindow(self, -1, style=wx.SP_3D)
splitter1.SetMinimumPaneSize(200)
splitter1.SetSashPosition(40)
splitter1.UpdateSize()
splitter2 = wx.SplitterWindow(splitter1, -1, style=wx.SP_3D)
splitter2.SetMinimumPaneSize(50)
splitter2.SetSashPosition(40)

self.sidebar = TreeServeur(splitter1, id=-1)

self.up =wx.TextCtrl(splitter2, wx.NewId(), "Un texte", size=(100, -1),pos=(80, 50))
self.down = wx.ListCtrl(splitter2, -1, style=wx.LC_LIST)

splitter1.SplitVertically(self.sidebar, splitter2,200)
splitter2.SplitHorizontally(self.up, self.down,0)

self.Centre()

def OnActivated(self,evt):
print "OnActivated: ", self.GetItemText(evt.GetItem())

def ShowMessage(self,msg):
d = wx.MessageDialog(self, msg, style=wx.OK)
d.ShowModal()
d.Destroy()

def OnAbout(self, event):
self.ShowMessage("About")

def OnExit(self,e):
self.Close(True)

def OnOpen(self,e):
#Ouverture d'un fichier"
self.dirname = ""
wildcard = "*.py"
dlg = wx.FileDialog(
self, message="Choose a file",
defaultDir=os.getcwd(),
defaultFile="",
wildcard=wildcard,
style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR
)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
f = open(path)
self.control.SetValue(f.read())
dlg.Destroy()

class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, "dragdrop.py")
frame.Show(True)
self.SetTopWindow(frame)
return True

app = MyApp(redirect=False)
app.MainLoop()


No comments:

Post a Comment